Class: Caboose::Product
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Product
- Defined in:
- app/models/caboose/product.rb
Constant Summary collapse
- STATUS_ACTIVE =
'Active'
- STATUS_INACTIVE =
'Inactive'
Class Method Summary collapse
Instance Method Summary collapse
- #active_customizations ⇒ Object
-
#as_json(options = {}) ⇒ Object
Instance Methods.
- #featured_image ⇒ Object
- #first_tiny_image_url ⇒ Object
- #in_stock ⇒ Object
- #input_required? ⇒ Boolean
- #live_variants ⇒ Object
- #most_popular_variant ⇒ Object
- #option1_values ⇒ Object
- #option1_values_with_media(url_only = false) ⇒ Object
- #option2_values ⇒ Object
- #option2_values_with_media(url_only = false) ⇒ Object
- #option3_values ⇒ Object
- #option3_values_with_media(url_only = false) ⇒ Object
- #options ⇒ Object
- #price_range ⇒ Object
- #price_varies ⇒ Object
- #related_items ⇒ Object
- #toggle_category(cat_id, value) ⇒ Object
- #update_on_sale ⇒ Object
- #url ⇒ Object
Class Method Details
.active ⇒ Object
70 71 72 |
# File 'app/models/caboose/product.rb', line 70 def self.active where(:status => 'Active') end |
.by_title ⇒ Object
74 75 76 |
# File 'app/models/caboose/product.rb', line 74 def self.by_title reorder('title') end |
.update_on_sale ⇒ Object
219 220 221 222 223 |
# File 'app/models/caboose/product.rb', line 219 def Product.update_on_sale Product.reorder(:id).all.each do |p| p.delay(:queue => 'caboose_store').update_on_sale end end |
.with_images ⇒ Object
Class Methods
66 67 68 |
# File 'app/models/caboose/product.rb', line 66 def self.with_images joins(:product_images) end |
Instance Method Details
#active_customizations ⇒ Object
149 150 151 |
# File 'app/models/caboose/product.rb', line 149 def active_customizations self.customizations.where(:status => 'Active') end |
#as_json(options = {}) ⇒ Object
Instance Methods
82 83 84 85 86 87 88 89 |
# File 'app/models/caboose/product.rb', line 82 def as_json(={}) self.attributes.merge({ :vendor => self.vendor, :categories => self.categories, :variants => self.variants, :images => self.product_images }) end |
#featured_image ⇒ Object
110 111 112 |
# File 'app/models/caboose/product.rb', line 110 def featured_image self.product_images.reject{|p| p.nil?}.first end |
#first_tiny_image_url ⇒ Object
105 106 107 108 |
# File 'app/models/caboose/product.rb', line 105 def first_tiny_image_url return '' if self.product_images.count == 0 return self.product_images.first.image.url(:tiny) end |
#in_stock ⇒ Object
141 142 143 |
# File 'app/models/caboose/product.rb', line 141 def in_stock Variant.where(:product_id => self.id).where('quantity_in_stock > 0').count > 0 end |
#input_required? ⇒ Boolean
145 146 147 |
# File 'app/models/caboose/product.rb', line 145 def input_required? !self.custom_input.nil? end |
#live_variants ⇒ Object
153 154 155 156 157 |
# File 'app/models/caboose/product.rb', line 153 def live_variants # Return variants that haven't been "deleted" self.variants.where(:status => ['Active', 'Inactive']) end |
#most_popular_variant ⇒ Object
101 102 103 |
# File 'app/models/caboose/product.rb', line 101 def most_popular_variant self.variants.where('price > ? AND status != ?', 0, 'Deleted').reorder('price ASC').first end |
#option1_values ⇒ Object
159 160 161 |
# File 'app/models/caboose/product.rb', line 159 def option1_values self.variants.where(:status => 'Active').reorder(:option1_sort_order).pluck(:option1).uniq.reject { |x| x.nil? || x.empty? } end |
#option1_values_with_media(url_only = false) ⇒ Object
163 164 165 166 167 |
# File 'app/models/caboose/product.rb', line 163 def option1_values_with_media(url_only = false) h = {} self.variants.where("status = ? and option1 is not null", 'Active').reorder(:option1_sort_order).each{ |v| h[v.option1] = url_only ? (v.option1_media && v.option1_media.image ? v.option1_media.image.url(:thumb) : false) : v.option1_media } return h end |
#option2_values ⇒ Object
169 170 171 |
# File 'app/models/caboose/product.rb', line 169 def option2_values self.variants.where(:status => 'Active').reorder(:option2_sort_order).pluck(:option2).uniq.reject { |x| x.nil? || x.empty? } end |
#option2_values_with_media(url_only = false) ⇒ Object
173 174 175 176 177 |
# File 'app/models/caboose/product.rb', line 173 def option2_values_with_media(url_only = false) h = {} self.variants.where("status = ? and option2 is not null", 'Active').reorder(:option2_sort_order).each{ |v| h[v.option2] = url_only ? (v.option2_media && v.option2_media.image ? v.option2_media.image.url(:thumb) : false) : v.option2_media } return h end |
#option3_values ⇒ Object
179 180 181 |
# File 'app/models/caboose/product.rb', line 179 def option3_values self.variants.where(:status => 'Active').reorder(:option3_sort_order).pluck(:option3).uniq.reject { |x| x.nil? || x.empty? } end |
#option3_values_with_media(url_only = false) ⇒ Object
183 184 185 186 187 |
# File 'app/models/caboose/product.rb', line 183 def option3_values_with_media(url_only = false) h = {} self.variants.where("status = ? and option3 is not null", 'Active').reorder(:option3_sort_order).each{ |v| h[v.option3] = url_only ? (v.option3_media && v.option3_media.image ? v.option3_media.image.url(:thumb) : false) : v.option3_media } return h end |
#options ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'app/models/caboose/product.rb', line 91 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
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/caboose/product.rb', line 119 def price_range min = 100000 max = 0 self.variants.each do |variant| next if variant.nil? || variant.price.nil? || variant.price <= 0 || variant.status != Variant::STATUS_ACTIVE price = variant.on_sale? ? variant.sale_price : variant.price min = price if price < min max = price if price > max end return [min, max] end |
#price_varies ⇒ Object
114 115 116 117 |
# File 'app/models/caboose/product.rb', line 114 def price_varies arr = variants.collect{ |v| v.price }.uniq return arr.count > 0 end |
#related_items ⇒ Object
137 138 139 |
# File 'app/models/caboose/product.rb', line 137 def Array.new # TODO should be obvious end |
#toggle_category(cat_id, value) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/models/caboose/product.rb', line 189 def toggle_category(cat_id, value) if value.to_i > 0 # Add to category if cat_id == 'all' CategoryMembership.where(:product_id => self.id).destroy_all Category.where(:site_id => self.site_id).reorder(:name).all.each{ |cat| CategoryMembership.create(:product_id => self.id, :category_id => cat.id) } else if !CategoryMembership.where(:product_id => self.id, :category_id => cat_id.to_i).exists? CategoryMembership.create(:product_id => self.id, :category_id => cat_id.to_i) end end else # Remove from category if cat_id == 'all' CategoryMembership.where(:product_id => self.id).destroy_all else CategoryMembership.where(:product_id => self.id, :category_id => cat_id.to_i).destroy_all end end end |
#update_on_sale ⇒ Object
208 209 210 211 212 213 214 215 216 217 |
# File 'app/models/caboose/product.rb', line 208 def update_on_sale is_on_sale = false self.variants.each do |v| is_on_sale = true if v.on_sale? end if (is_on_sale && !self.on_sale) || (!is_on_sale && self.on_sale) self.on_sale = is_on_sale self.save end end |
#url ⇒ Object
133 134 135 |
# File 'app/models/caboose/product.rb', line 133 def url "/products/#{self.id}" end |