Class: Product

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/product.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) available_shipping_rates

these the the shipping rates per the shipping address on the order



32
33
34
# File 'app/models/product.rb', line 32

def available_shipping_rates
  @available_shipping_rates
end

Class Method Details

+ (Object) active



166
167
168
169
170
# File 'app/models/product.rb', line 166

def self.active
  where("products.deleted_at IS NULL OR products.deleted_at > ?", Time.zone.now)
  #  Add this line if you want the available_at to function
  #where("products.available_at IS NULL OR products.available_at >= ?", Time.zone.now)
end

+ (Array[Product]) admin_grid(params = {}, active_state = nil)

paginated results from the admin products grid

Parameters:

  • (Optional params)
  • the (Optional Boolean)

    state of the product you are searching (active == true)

Returns:



201
202
203
204
205
206
207
208
209
210
211
# File 'app/models/product.rb', line 201

def self.admin_grid(params = {}, active_state = nil)
  grid = includes(:variants)
  grid = grid.where(['products.deleted_at IS NOT NULL AND products.deleted_at > ?', Time.now.to_s(:db)])  if active_state == false##  note nil != false
  grid = grid.where(['products.deleted_at IS NULL     OR  products.deleted_at < ?', Time.now.to_s(:db)])  if active_state == true
  grid = grid.where("products.name LIKE ?",                 "#{params[:name]}%")                  if params[:name].present?
  grid = grid.where("products.product_type_id = ?",      params[:product_type_id])       if params[:product_type_id].present?
  grid = grid.where("products.shipping_category_id = ?", params[:shipping_category_id])  if params[:shipping_category_id].present?
  grid = grid.where("products.available_at > ?",         params[:available_at_gt])       if params[:available_at_gt].present?
  grid = grid.where("products.available_at < ?",         params[:available_at_lt])       if params[:available_at_lt].present?
  grid
end

This returns the first featured product in the database, if there isn't a featured product the first product will be returned

Parameters:

  • (none)

Returns:



161
162
163
164
# File 'app/models/product.rb', line 161

def self.featured
  product = where({ :products => {:featured => true} } ).includes(:images).first
  product ? product : includes(:images).where(['products.deleted_at IS NULL']).first
end

+ (Product) standard_search(args)

Solr searching for products

Parameters:

  • (args)
  • :rows, (params)

    :page

Returns:



150
151
152
153
154
# File 'app/models/product.rb', line 150

def self.standard_search(args)
    Product.includes( [:properties, :images]).
            where(['products.name LIKE ? OR products.meta_keywords LIKE ?', "%#{args}%", "%#{args}%"]).
            where(['products.deleted_at IS NULL OR products.deleted_at > ?', Time.zone.now])
end

Instance Method Details

- (Object) activate!



179
180
181
182
# File 'app/models/product.rb', line 179

def activate!
  self.deleted_at = nil
  save
end

- (Object) active(at = Time.zone.now)



172
173
174
# File 'app/models/product.rb', line 172

def active(at = Time.zone.now)
  deleted_at.nil? || deleted_at >= at
end

- (Boolean) active?(at = Time.zone.now)

Returns:

  • (Boolean)


175
176
177
# File 'app/models/product.rb', line 175

def active?(at = Time.zone.now)
  active(at)
end

- (Boolean) available?

Returns:

  • (Boolean)


184
185
186
# File 'app/models/product.rb', line 184

def available?
  active
end

- (String) brand_name

returns the brand's name or a blank string

ex: obj.brand_name => 'Nike'

Parameters:

  • (none)

Returns:

  • (String)


193
194
195
# File 'app/models/product.rb', line 193

def brand_name
  brand_id ? brand.name : ''
end

- (String) display_price_range(j = ' to ')

range of the product prices in plain english

Parameters:

  • separator (Optional String)

    between the low and high price

Returns:

  • (String)

    Low price + separator + High price



122
123
124
# File 'app/models/product.rb', line 122

def display_price_range(j = ' to ')
  price_range.join(j)
end

Image that is featured for your product

Parameters:

  • the (Optional Symbol)

    size of the image expected back

Returns:

  • (String)

    name of the file to show from the public folder



89
90
91
# File 'app/models/product.rb', line 89

def featured_image(image_size = :small)
  images.first ? images.first.photo.url(image_size) : "no_image_#{image_size.to_s}.jpg"
end

- (Object) hero_variant



71
72
73
# File 'app/models/product.rb', line 71

def hero_variant
  active_variants.detect{|v| v.master } || active_variants.first
end

- (Decimal) price

Price of cheapest variant

Parameters:

  • the (none)

    size of the image expected back

Returns:

  • (Decimal)

    price



97
98
99
# File 'app/models/product.rb', line 97

def price
  active_variants.present? ? price_range.first : raise( VariantRequiredError )
end

- (Array) price_range

range of the product prices (Just teh low and high price) as an array

Parameters:

  • (none)

Returns:

  • (Array)
    Low price, High price


130
131
132
133
134
# File 'app/models/product.rb', line 130

def price_range
  return @price_range if @price_range
  return @price_range = ['N/A', 'N/A'] if active_variants.empty?
  @price_range = active_variants.minmax {|a,b| a.price <=> b.price }.map(&:price)
end

- (Boolean) price_range?

Answers if the product has a price range or just one price.

if there is more than one price returns true

Parameters:

  • (none)

Returns:

  • (Boolean)

    true == there is more than one price



141
142
143
# File 'app/models/product.rb', line 141

def price_range?
  !(price_range.first == price_range.last)
end

- (String) set_keywords

method used by forms to set the array of keywords separated by a comma

Parameters:

  • (none)

Returns:

  • (String)

    product_keywords separated by comma



114
115
116
# File 'app/models/product.rb', line 114

def set_keywords
  product_keywords ? product_keywords.join(', ') : ''
end

- (none) set_keywords=(value)

in the admin form this is the method called when the form is submitted, The method sets the product_keywords attribute to an array of these values

Parameters:

  • value (String)

    for set_keywords in a products form

Returns:

  • (none)


106
107
108
# File 'app/models/product.rb', line 106

def set_keywords=(value)
  self.product_keywords = value ? value.split(',').map{|w| w.strip} : []
end

- (TaxRate) tax_rate(region_id, time = Time.zone.now)

gives you the tax rate for the give state_id and the time.

Tax rates can change from year to year so Time is a factor

Parameters:

  • state.id (Integer)
  • Time (Optional Time)

    now if no value is passed in

Returns:

  • (TaxRate)

    TaxRate for the state at a given time



81
82
83
# File 'app/models/product.rb', line 81

def tax_rate(region_id, time = Time.zone.now)
  TaxRate.for_region(region_id).at(time).active.order('start_date DESC').first
end