Skip to content

Commit

Permalink
Clean up checks to avoid db connection on precompile
Browse files Browse the repository at this point in the history
We still need to deal with the Preference / Config issue as stated in spree#3833
  • Loading branch information
huoxito committed Oct 14, 2013
1 parent b9e52a2 commit 966525c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 52 deletions.
3 changes: 2 additions & 1 deletion core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Product < ActiveRecord::Base
has_many :stock_items, through: :variants_including_master

delegate_belongs_to :master, :sku, :price, :currency, :display_amount, :display_price, :weight, :height, :width, :depth, :is_master, :has_default_price?, :cost_currency, :price_in, :amount_in
delegate_belongs_to :master, :cost_price if Variant.table_exists? && Variant.column_names.include?('cost_price')

delegate_belongs_to :master, :cost_price

after_create :set_master_variant_defaults
after_create :add_properties_and_option_types_from_prototype
Expand Down
8 changes: 2 additions & 6 deletions core/app/models/spree/product/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def self.add_simple_scopes(scopes)
# We should not define price scopes here, as they require something slightly different
next if name.to_s.include?("master_price")
parts = name.to_s.match(/(.*)_by_(.*)/)
order_text = "#{Product.quoted_table_name}.#{parts[2]} #{parts[1] == 'ascend' ? "ASC" : "DESC"}"
self.scope(name.to_s, -> { relation.order(order_text) })
self.scope(name.to_s, -> { relation.order("#{Product.quoted_table_name}.#{parts[2]} #{parts[1] == 'ascend' ? "ASC" : "DESC"}") })
end
end

Expand Down Expand Up @@ -212,10 +211,7 @@ def self.active(currency = nil)
# problem shown in #1247.
def self.group_by_products_id
if (ActiveRecord::Base.connection.adapter_name == 'PostgreSQL')
# Need to check, otherwise `column_names` will fail
if table_exists?
group(column_names.map { |col_name| "#{table_name}.#{col_name}"})
end
group(column_names.map { |col_name| "#{table_name}.#{col_name}"})
else
group("#{self.quoted_table_name}.id")
end
Expand Down
5 changes: 3 additions & 2 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ class Variant < ActiveRecord::Base
class_name: 'Spree::Price',
dependent: :destroy

delegate_belongs_to :default_price, :display_price, :display_amount, :price, :price=, :currency if Spree::Price.table_exists?
delegate_belongs_to :default_price, :display_price, :display_amount, :price, :price=, :currency

has_many :prices,
class_name: 'Spree::Price',
dependent: :destroy

validate :check_price
validates :price, numericality: { greater_than_or_equal_to: 0 }, presence: true, if: proc { Spree::Config[:require_master_price] }
validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true } if self.table_exists? && self.column_names.include?('cost_price')

validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true }

before_validation :set_cost_currency
after_save :save_default_price
Expand Down
6 changes: 1 addition & 5 deletions core/lib/spree/core/permalinks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ def make_permalink(options={})
options[:field] ||= :permalink
self.permalink_options = options

if self.connected?
if self.table_exists? && self.column_names.include?(permalink_options[:field].to_s)
before_validation(:on => :create) { save_permalink }
end
end
before_validation(:on => :create) { save_permalink }
end

def find_by_param(value, *args)
Expand Down
72 changes: 34 additions & 38 deletions core/lib/spree/core/product_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,26 @@ def ProductFilters.price_filter
# the (uniquely named) field "p_brand.value". There's also a test for brand info
# being blank: note that this relies on with_property doing a left outer join
# rather than an inner join.
if Spree::Property.table_exists?
Spree::Product.add_search_scope :brand_any do |*opts|
conds = opts.map {|o| ProductFilters.brand_filter[:conds][o]}.reject { |c| c.nil? }
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
Spree::Product.with_property('brand').where(scope)
Spree::Product.add_search_scope :brand_any do |*opts|
conds = opts.map {|o| ProductFilters.brand_filter[:conds][o]}.reject { |c| c.nil? }
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
Spree::Product.with_property('brand').where(scope)
end

def ProductFilters.brand_filter
brand_property = Spree::Property.find_by(name: 'brand')
brands = brand_property ? Spree::ProductProperty.where(property_id: brand_property.id).pluck(:value).uniq.map(&:to_s) : []
pp = Spree::ProductProperty.arel_table
conds = Hash[*brands.map { |b| [b, pp[:value].eq(b)] }.flatten]
{
name: 'Brands',
scope: :brand_any,
conds: conds,
labels: (brands.sort).map { |k| [k, k] }
}
end
def ProductFilters.brand_filter
brand_property = Spree::Property.find_by(name: 'brand')
brands = brand_property ? Spree::ProductProperty.where(property_id: brand_property.id).pluck(:value).uniq.map(&:to_s) : []
pp = Spree::ProductProperty.arel_table
conds = Hash[*brands.map { |b| [b, pp[:value].eq(b)] }.flatten]
{
name: 'Brands',
scope: :brand_any,
conds: conds,
labels: (brands.sort).map { |k| [k, k] }
}
end

# Example: a parameterized filter
Expand All @@ -140,25 +138,23 @@ def ProductFilters.brand_filter
#
# The brand-finding code can be simplified if a few more named scopes were added to
# the product properties model.
if Spree::Property.table_exists?
Spree::Product.add_search_scope :selective_brand_any do |*opts|
Spree::Product.brand_any(*opts)
end
Spree::Product.add_search_scope :selective_brand_any do |*opts|
Spree::Product.brand_any(*opts)
end

def ProductFilters.selective_brand_filter(taxon = nil)
taxon ||= Spree::Taxonomy.first.root
brand_property = Spree::Property.find_by(name: 'brand')
scope = Spree::ProductProperty.where(property: brand_property).
joins(product: :taxons).
where("#{Spree::Taxon.table_name}.id" => [taxon] + taxon.descendants).
scoped
brands = scope.pluck(:value).uniq
{
name: 'Applicable Brands',
scope: :selective_brand_any,
labels: brands.sort.map { |k| [k, k] }
}
end
def ProductFilters.selective_brand_filter(taxon = nil)
taxon ||= Spree::Taxonomy.first.root
brand_property = Spree::Property.find_by(name: 'brand')
scope = Spree::ProductProperty.where(property: brand_property).
joins(product: :taxons).
where("#{Spree::Taxon.table_name}.id" => [taxon] + taxon.descendants).
scoped
brands = scope.pluck(:value).uniq
{
name: 'Applicable Brands',
scope: :selective_brand_any,
labels: brands.sort.map { |k| [k, k] }
}
end

# Provide filtering on the immediate children of a taxon
Expand Down

0 comments on commit 966525c

Please sign in to comment.