Class: Caboose::Product

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

Constant Summary collapse

STATUS_ACTIVE =
'Active'
STATUS_INACTIVE =
'Inactive'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activeObject



66
67
68
# File 'app/models/caboose/product.rb', line 66

def self.active
  where(:status => 'Active')
end

.by_titleObject



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

def self.by_title
  order('title')
end

.with_imagesObject

Class Methods



62
63
64
# File 'app/models/caboose/product.rb', line 62

def self.with_images
  joins(:product_images)
end

Instance Method Details

#active_customizationsObject



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

def active_customizations
  self.customizations.where(:status => 'Active')
end

#as_json(options = {}) ⇒ Object

Instance Methods



78
79
80
81
82
83
84
85
# File 'app/models/caboose/product.rb', line 78

def as_json(options={})
  self.attributes.merge({
    :vendor => self.vendor,
    :categories => self.categories,
    :variants => self.variants,
    :images => self.product_images        
  })
end


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

def featured_image
	self.product_images.reject{|p| p.nil?}.first
end

#first_tiny_image_urlObject



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

def first_tiny_image_url
	return '' if self.product_images.count == 0
	return self.product_images.first.image.url(:tiny)
end

#in_stockObject



137
138
139
# File 'app/models/caboose/product.rb', line 137

def in_stock
  Variant.where(:product_id => self.id).where('quantity_in_stock > 0').count > 0
end

#input_required?Boolean

Returns:

  • (Boolean)


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

def input_required?
  !self.custom_input.nil?
end

#live_variantsObject



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

def live_variants
  
  # Return variants that haven't been "deleted"
  self.variants.where(:status => ['Active', 'Inactive'])
end


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

def most_popular_variant
  self.variants.where('price > ? AND status != ?', 0, 'Deleted').order('price ASC').first
end

#option1_valuesObject



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

def option1_values      
  self.variants.where(:status => 'Active').reorder(:option1_sort_order).pluck(:option1).uniq.reject { |x| x.nil? || x.empty? }
end

#option2_valuesObject



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

def option2_values
  self.variants.where(:status => 'Active').reorder(:option2_sort_order).pluck(:option2).uniq.reject { |x| x.nil? || x.empty? }
end

#option3_valuesObject



163
164
165
# File 'app/models/caboose/product.rb', line 163

def option3_values
  self.variants.where(:status => 'Active').reorder(:option3_sort_order).pluck(:option3).uniq.reject { |x| x.nil? || x.empty? }
end

#optionsObject



87
88
89
90
91
92
93
94
95
# File 'app/models/caboose/product.rb', line 87

def options
  options = []
  
  options << self.option1 if !self.option1.nil? && self.option1.strip.length > 0
  options << self.option2 if !self.option2.nil? && self.option2.strip.length > 0
  options << self.option3 if !self.option3.nil? && self.option3.strip.length > 0
  
  return options
end

#price_rangeObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/caboose/product.rb', line 115

def price_range
  min = 100000
  max = 0
  
  self.variants.each do |variant|        
    next if variant.nil? || variant.price.nil? || variant.price <= 0
    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_variesObject



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

def price_varies
  arr = variants.collect{ |v| v.price }.uniq
  return arr.count > 0
end


133
134
135
# File 'app/models/caboose/product.rb', line 133

def related_items
  Array.new # TODO should be obvious
end

#toggle_category(cat_id, value) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/caboose/product.rb', line 167

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_saleObject



186
187
188
189
190
191
192
193
194
195
# File 'app/models/caboose/product.rb', line 186

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

#urlObject



129
130
131
# File 'app/models/caboose/product.rb', line 129

def url
  "/products/#{self.id}"
end