Class: MageRecord::Product

Inherits:
EavRecord
  • Object
show all
Defined in:
lib/magerecord/fitlion/product.rb,
lib/magerecord/product.rb

Overview

add custom FitLion-specific product methods

Instance Method Summary collapse

Methods inherited from EavRecord

eav_attributes, eav_prefix, #method_missing, #respond_to?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MageRecord::EavRecord

Instance Method Details

#attributesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/magerecord/fitlion/product.rb', line 8

def attributes
  if @attribs.nil?
    @attribs = {}

    # only simple/virtual products will have custom attributes
    if %w{simple virtual}.include?(type_id)
      # get all custom product attributes specific to attribute set (except "brand")
      attribs = set.downcase.gsub(/.*\((.+)\)/, "\\1").gsub(/ only/, '').split(' + ') - ['brand']

      attribs.map{ |a| a.split(' ').join('_') }.each do |attrib|
        @attribs[attrib] = send(attrib)
      end
    end
  end

  @attribs
end

#enabled?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/magerecord/product.rb', line 26

def enabled?
  # note: a magento product's enabled/disabled status is stored in EAV attribute code "status"
  # 1 = enabled, 2 = disabled
  status.to_i == 1
end

#full_name(with_brand = true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/magerecord/fitlion/product.rb', line 26

def full_name(with_brand = true)
  if @fname.nil?
    @fname = "#{name} (#{uom})"

    # note: most bundles are not associated with any brand
    @fname = "(#{brand}) #{@fname}" if with_brand and brand

    @fname = "#{@fname}#{attributes.values.map{ |a| " (#{a})" }.join}"
  end

  # return cached full name
  @fname
end

#in_stock?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/magerecord/product.rb', line 21

def in_stock?
  stock.is_in_stock?
end

#is_supplement?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/magerecord/fitlion/product.rb', line 40

def is_supplement?
  set.downcase.include? 'supplement'
end

#qtyObject



16
17
18
# File 'lib/magerecord/product.rb', line 16

def qty
  stock.qty.to_i
end

#setObject



33
34
35
36
# File 'lib/magerecord/product.rb', line 33

def set
  # return product's attribute set
  product_attribute_set.attribute_set_name
end

#uomObject



4
5
6
# File 'lib/magerecord/fitlion/product.rb', line 4

def uom
  "#{measurement} #{unit}"
end