Class: Product

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

Direct Known Subclasses

Pack

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/models/product.rb', line 193

def method_missing(method_id,*args,&block)
  method_name = method_id.to_s
  if cat_id = method_name.match(/has_category_(\d+)/)
    return categories.find_by_id(cat_id[1].to_i, :select => 'id').present?
  elsif custom_attribute = load_custom_attribute(method_name, ['_value'])
    read_custom_attribute(custom_attribute, args.first || :value)
  elsif custom_attribute = load_custom_attribute(method_name, ['='])
    write_custom_attribute(custom_attribute, args.first)
  else
    super
  end
end

Instance Attribute Details

#price_variationObject

Returns the value of attribute price_variation.



3
4
5
# File 'app/models/product.rb', line 3

def price_variation
  @price_variation
end

#price_variation_idObject

Returns the value of attribute price_variation_id.



3
4
5
# File 'app/models/product.rb', line 3

def price_variation_id
  @price_variation_id
end

#special_offer_discountObject

Returns the value of attribute special_offer_discount.



3
4
5
# File 'app/models/product.rb', line 3

def special_offer_discount
  @special_offer_discount
end

#special_offer_discount_priceObject

Returns the value of attribute special_offer_discount_price.



3
4
5
# File 'app/models/product.rb', line 3

def special_offer_discount_price
  @special_offer_discount_price
end

#voucher_discountObject

Returns the value of attribute voucher_discount.



3
4
5
# File 'app/models/product.rb', line 3

def voucher_discount
  @voucher_discount
end

#voucher_discount_priceObject

Returns the value of attribute voucher_discount_price.



3
4
5
# File 'app/models/product.rb', line 3

def voucher_discount_price
  @voucher_discount_price
end

Class Method Details

.get_offer_monthObject

Returns month’s offers



76
77
78
# File 'app/models/product.rb', line 76

def self.get_offer_month
  find_by_active_and_deleted_and_offer_month(true,false,true)
end

.get_on_first_pageObject

Returns the first page products



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

def self.get_on_first_page
  find_all_by_active_and_deleted_and_on_first_page(true,false,true)
end

Instance Method Details

#activateObject



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

def activate
  self.update_attribute('active', !self.active? )
end

#attribute_of(attribute) ⇒ Object



85
86
87
# File 'app/models/product.rb', line 85

def attribute_of(attribute)
  attribute_values.find_by_attribute_id(attribute.id)
end

#cloneObject



89
90
91
92
93
94
95
96
97
98
# File 'app/models/product.rb', line 89

def clone
  product_cloned = super
  product_cloned.meta_info = meta_info.clone if meta_info
  product_cloned.translations = translations.clone unless translations.empty?
  product_cloned.dynamic_attribute_values = dynamic_attribute_values.collect(&:clone)
  %w(attachment_ids picture_ids tag_list category_ids attribute_value_ids).each do |assoc|
    product_cloned.send(assoc+'=', self.send(assoc))
  end
  return product_cloned
end

#discountObject



159
160
161
# File 'app/models/product.rb', line 159

def discount
  self.special_offer_discount_price || 0.0
end

#has_discount?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/models/product.rb', line 144

def has_discount?
  not discount.zero?
end

#has_special_offers?Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/models/product.rb', line 174

def has_special_offers?
  if self.old_price != self.price
    return true
  else
    selected_products = []
    engine :special_offer_engine do |e|
      begin
        rule_builder = SpecialOffer.new(e)
        rule_builder.selected_products = selected_products
        rule_builder.rules
      rescue Exception
      end
      e.assert self
      e.match
    end
    return !selected_products.blank?
  end
end

#old_priceObject



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

def old_price
  price(:voucher_discount => false, :special_offer_discount => false)
end

#packaging_priceObject



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

def packaging_price
  #TODO get real variable
  return 0
end

#price(*args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/product.rb', line 119

def price(*args)
  passed_options = args.extract_options!
  options = {
    :tax => true,
    :voucher_discount => true,
    :special_offer_discount => true,
    :variation => false,
    :packaging => false
  }.update(passed_options.symbolize_keys)

  price = read_attribute(:price) || 0.0
  price += tax(false)
  price -= self.discount if options[:special_offer_discount]
  price -= self.voucher_discount_price || 0.0 if options[:voucher_discount]
  price -= (price * price_variation.discount).to_f / 100.0 if options[:variation] && price_variation
  price += self.packaging_price.to_f || 0.0 if options[:packaging]
  price = 0.0 if price < 0

  price
end

#price_to_s(*args) ⇒ Object

Returns price’s string with currency symbol

This method is an overload of price attribute.

Parameters

  • :with_tax - false by defaults. Returns price with tax if true

  • :with_currency - true by defaults. The currency of user is considered if true



155
156
157
# File 'app/models/product.rb', line 155

def price_to_s(*args)
  price(args).to_s
end

#redirection_product_with_deletedObject



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

def redirection_product_with_deleted
  return (redirection_product_without_deleted && redirection_product_without_deleted.deleted? ? redirection_product_without_deleted.redirection_product : redirection_product_without_deleted)
end

#respond_to?(method, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
209
210
211
212
213
# File 'app/models/product.rb', line 206

def respond_to?(method, include_priv = false)
  method_name = method.to_s
  if load_custom_attribute(method_name)
    true
  else
    super
  end
end

#soft_deleteObject



110
111
112
# File 'app/models/product.rb', line 110

def soft_delete
  self.update_attribute('deleted', !self.deleted? )
end

#synchronize_stockObject



100
101
102
103
104
# File 'app/models/product.rb', line 100

def synchronize_stock
  if stop_sales? && active? && stock.to_i < 1
    self.update_attribute('active', false)
  end
end

#tax(with_currency = true) ⇒ Object

Returns total product’s tax

Parameters

  • :with_currency - true by defaults. The currency of user is considered if true

This method use price : price(false, with_currency)



169
170
171
172
# File 'app/models/product.rb', line 169

def tax(with_currency=true)
  return 0.0
  #return ("%01.2f" % (price(false, with_currency) * self.rate_tax/100)).to_f
end