Temporary/demonstration caching behaviour, INCOMPLETE

This commit is contained in:
Will Marshall
2014-07-02 10:26:19 +10:00
committed by Rohan Mitchell
parent 3da2461af5
commit 8b439c4e69
8 changed files with 64 additions and 14 deletions

View File

@@ -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

View File

@@ -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."

View File

@@ -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

View File

@@ -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

View File

@@ -1,4 +1,7 @@
class Api::AddressSerializer < ActiveModel::Serializer
cached
delegate :cache_key, to: :object
attributes :id, :zipcode, :city, :state
def state

View File

@@ -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

View File

@@ -1,4 +1,7 @@
class Api::TaxonSerializer < ActiveModel::Serializer
cached
delegate :cache_key, to: :object
attributes :id, :name, :permalink, :icon
def icon

View File

@@ -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