diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 5b922b15e4..d42031273d 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -7,7 +7,7 @@ class Enterprise < ActiveRecord::Base has_many :producer_properties, foreign_key: 'producer_id' has_many :supplied_products, :class_name => 'Spree::Product', :foreign_key => 'supplier_id', :dependent => :destroy has_many :distributed_orders, :class_name => 'Spree::Order', :foreign_key => 'distributor_id' - belongs_to :address, :class_name => 'Spree::Address' + belongs_to :address, :class_name => 'Spree::Address', touch: true has_many :product_distributions, :foreign_key => 'distributor_id', :dependent => :destroy has_many :distributed_products, :through => :product_distributions, :source => :product has_many :enterprise_fees diff --git a/app/models/enterprise_relationship.rb b/app/models/enterprise_relationship.rb index 26bf8fc012..4db650f2e7 100644 --- a/app/models/enterprise_relationship.rb +++ b/app/models/enterprise_relationship.rb @@ -1,6 +1,6 @@ class EnterpriseRelationship < ActiveRecord::Base - belongs_to :parent, class_name: 'Enterprise' - belongs_to :child, class_name: 'Enterprise' + belongs_to :parent, class_name: 'Enterprise', touch: true + belongs_to :child, class_name: 'Enterprise', touch: true validates_presence_of :parent_id, :child_id validates_uniqueness_of :child_id, scope: :parent_id, message: "^That relationship is already established." diff --git a/app/models/spree/classification_decorator.rb b/app/models/spree/classification_decorator.rb index 7e34e6890e..8fa201528c 100644 --- a/app/models/spree/classification_decorator.rb +++ b/app/models/spree/classification_decorator.rb @@ -1,4 +1,6 @@ Spree::Classification.class_eval do + belongs_to :product, :class_name => "Spree::Product", touch: true + belongs_to :taxon, :class_name => "Spree::Taxon", touch: true before_destroy :dont_destroy_if_primary_taxon def dont_destroy_if_primary_taxon diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 22fa5b3b8b..443a9dd8a9 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -4,8 +4,8 @@ Spree::Product.class_eval do # https://github.com/rails/rails/issues/7618 has_many :option_types, :through => :product_option_types, :dependent => :destroy - belongs_to :supplier, :class_name => 'Enterprise' - belongs_to :primary_taxon, class_name: 'Spree::Taxon' + belongs_to :supplier, :class_name => 'Enterprise', touch: true + belongs_to :primary_taxon, class_name: 'Spree::Taxon', touch: true has_many :product_distributions, :dependent => :destroy has_many :distributors, :through => :product_distributions diff --git a/app/serializers/api/address_serializer.rb b/app/serializers/api/address_serializer.rb index a4dfcb0f5f..98344e0315 100644 --- a/app/serializers/api/address_serializer.rb +++ b/app/serializers/api/address_serializer.rb @@ -1,4 +1,7 @@ class Api::AddressSerializer < ActiveModel::Serializer + cached + delegate :cache_key, to: :object + attributes :id, :zipcode, :city, :state def state diff --git a/app/serializers/api/enterprise_serializer.rb b/app/serializers/api/enterprise_serializer.rb index a74606dd79..c0b79084f0 100644 --- a/app/serializers/api/enterprise_serializer.rb +++ b/app/serializers/api/enterprise_serializer.rb @@ -1,9 +1,58 @@ class Api::EnterpriseSerializer < ActiveModel::Serializer + # To improve this: http://hawkins.io/2013/06/caching_object_graphs_with_active_model_serializers/ + # + # Taxons: + # classifications touch products + # products touch suppliers + # Taxon could change but unlikely: if becomes a problem dereference + # + # Relatives: + # Enterprise_relationships touches parent, child + # Otherwise dereferencing makes easy + # + # Address: + # Fine + # + # Shipping Methods: + # Create distributors_shipping_methods class + # Set up touches + + def serializable_hash + cached_serializer_hash.merge uncached_serializer_hash + end + + private + + def cached_serializer_hash + Api::CachedEnterpriseSerializer.new(object, @options).serializable_hash + end + + def uncached_serializer_hash + Api::UncachedEnterpriseSerializer.new(object, @options).serializable_hash + end +end + +class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer + attributes :orders_close_at, :active + + def orders_close_at + OrderCycle.first_closing_for(object).andand.orders_close_at + end + + def active + @options[:active_distributors].andand.include?(object) + end +end + +class Api::CachedEnterpriseSerializer < ActiveModel::Serializer + cached + delegate :cache_key, to: :object + attributes :name, :id, :description, :latitude, :longitude, :long_description, :website, :instagram, :linkedin, :twitter, :facebook, :is_primary_producer, :is_distributor, :phone, :visible, :email, :hash, :logo, :promo_image, :icon, :path, - :pickup, :delivery, :active, :orders_close_at + :pickup, :delivery has_many :distributed_taxons, key: :taxons, serializer: Api::TaxonSerializer has_many :supplied_taxons, serializer: Api::TaxonSerializer @@ -20,14 +69,6 @@ class Api::EnterpriseSerializer < ActiveModel::Serializer object.shipping_methods.where(:require_ship_address => true).present? end - def active - @options[:active_distributors].andand.include?(object) - end - - def orders_close_at - OrderCycle.first_closing_for(object).andand.orders_close_at - end - def email object.email.to_s.reverse end diff --git a/app/serializers/api/taxon_serializer.rb b/app/serializers/api/taxon_serializer.rb index a907532a6b..352a158e95 100644 --- a/app/serializers/api/taxon_serializer.rb +++ b/app/serializers/api/taxon_serializer.rb @@ -1,4 +1,7 @@ class Api::TaxonSerializer < ActiveModel::Serializer + cached + delegate :cache_key, to: :object + attributes :id, :name, :permalink, :icon def icon diff --git a/config/environments/development.rb b/config/environments/development.rb index 62642f7809..efa229e33c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -5,6 +5,7 @@ Openfoodnetwork::Application.configure do # every request. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. config.cache_classes = false + config.cache_store = :memory_store # Log error messages when you accidentally call methods on nil. config.whiny_nils = true