Class: CabooseStore::Product
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CabooseStore::Product
- Defined in:
- app/models/caboose_store/product.rb
Class Method Summary collapse
- .active ⇒ Object
- .by_title ⇒ Object
-
.with_images ⇒ Object
Class Methods.
Instance Method Summary collapse
- #active_customizations ⇒ Object
-
#as_json(options = {}) ⇒ Object
Instance Methods.
- #featured_image ⇒ Object
- #in_stock ⇒ Object
- #input_required? ⇒ Boolean
- #live_variants ⇒ Object
- #most_popular_variant ⇒ Object
- #option1_values ⇒ Object
- #option2_values ⇒ Object
- #option3_values ⇒ Object
- #options ⇒ Object
- #price_range ⇒ Object
- #price_varies ⇒ Object
- #related_items ⇒ Object
- #url ⇒ Object
Class Method Details
.active ⇒ Object
52 53 54 |
# File 'app/models/caboose_store/product.rb', line 52 def self.active where(:status => 'Active') end |
.by_title ⇒ Object
56 57 58 |
# File 'app/models/caboose_store/product.rb', line 56 def self.by_title order('title') end |
.with_images ⇒ Object
Class Methods
48 49 50 |
# File 'app/models/caboose_store/product.rb', line 48 def self.with_images joins(:product_images) end |
Instance Method Details
#active_customizations ⇒ Object
123 124 125 |
# File 'app/models/caboose_store/product.rb', line 123 def active_customizations self.customizations.where(:status => 'Active') end |
#as_json(options = {}) ⇒ Object
Instance Methods
64 65 66 67 68 69 |
# File 'app/models/caboose_store/product.rb', line 64 def as_json(={}) self.attributes.merge({ :variants => self.variants, :images => self.product_images }) end |
#featured_image ⇒ Object
85 86 87 |
# File 'app/models/caboose_store/product.rb', line 85 def featured_image self.product_images.reject{|p| p.nil?}.first end |
#in_stock ⇒ Object
115 116 117 |
# File 'app/models/caboose_store/product.rb', line 115 def in_stock Variant.where(:product_id => self.id).where('quantity_in_stock > 0').count > 0 end |
#input_required? ⇒ Boolean
119 120 121 |
# File 'app/models/caboose_store/product.rb', line 119 def input_required? !self.custom_input.nil? end |
#live_variants ⇒ Object
127 128 129 130 131 |
# File 'app/models/caboose_store/product.rb', line 127 def live_variants # Return variants that haven't been "deleted" self.variants.where(:status => ['Active', 'Inactive']) end |
#most_popular_variant ⇒ Object
81 82 83 |
# File 'app/models/caboose_store/product.rb', line 81 def most_popular_variant self.variants.where('price > ? AND status != ?', 0, 'Deleted').order('price ASC').first end |
#option1_values ⇒ Object
133 134 135 |
# File 'app/models/caboose_store/product.rb', line 133 def option1_values self.variants.where(:status => 'Active').reorder(:option1_sort_order).pluck(:option1).uniq.reject { |x| x.nil? || x.empty? } end |
#option2_values ⇒ Object
137 138 139 |
# File 'app/models/caboose_store/product.rb', line 137 def option2_values self.variants.where(:status => 'Active').reorder(:option2_sort_order).pluck(:option2).uniq.reject { |x| x.nil? || x.empty? } end |
#option3_values ⇒ Object
141 142 143 |
# File 'app/models/caboose_store/product.rb', line 141 def option3_values self.variants.where(:status => 'Active').reorder(:option3_sort_order).pluck(:option3).uniq.reject { |x| x.nil? || x.empty? } end |
#options ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'app/models/caboose_store/product.rb', line 71 def = [] << self.option1 if !self.option1.nil? && self.option1.strip.length > 0 << self.option2 if !self.option2.nil? && self.option2.strip.length > 0 << self.option3 if !self.option3.nil? && self.option3.strip.length > 0 return end |
#price_range ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/caboose_store/product.rb', line 94 def price_range min = 100000 max = 0 self.variants.each do |variant| next if variant.nil? or variant.price.nil? or variant.price <= 0 min = variant.price if variant.price < min max = variant.price if variant.price > max end return [min, max] end |
#price_varies ⇒ Object
89 90 91 92 |
# File 'app/models/caboose_store/product.rb', line 89 def price_varies arr = variants.collect{ |v| v.price }.uniq return arr.count > 0 end |
#related_items ⇒ Object
111 112 113 |
# File 'app/models/caboose_store/product.rb', line 111 def Array.new # TODO should be obvious end |
#url ⇒ Object
107 108 109 |
# File 'app/models/caboose_store/product.rb', line 107 def url "/products/#{self.id}" end |