Class: Caboose::Product

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activeObject



55
56
57
# File 'app/models/caboose/product.rb', line 55

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

.by_titleObject



59
60
61
# File 'app/models/caboose/product.rb', line 59

def self.by_title
  order('title')
end

.with_imagesObject

Class Methods



51
52
53
# File 'app/models/caboose/product.rb', line 51

def self.with_images
  joins(:product_images)
end

Instance Method Details

#active_customizationsObject



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

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

#as_json(options = {}) ⇒ Object

Instance Methods



67
68
69
70
71
72
73
74
# File 'app/models/caboose/product.rb', line 67

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


95
96
97
# File 'app/models/caboose/product.rb', line 95

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

#first_tiny_image_urlObject



90
91
92
93
# File 'app/models/caboose/product.rb', line 90

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

#in_stockObject



125
126
127
# File 'app/models/caboose/product.rb', line 125

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

#input_required?Boolean

Returns:

  • (Boolean)


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

def input_required?
  !self.custom_input.nil?
end

#live_variantsObject



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

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


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

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

#option1_valuesObject



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

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

#option2_valuesObject



147
148
149
# File 'app/models/caboose/product.rb', line 147

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

#option3_valuesObject



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

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

#optionsObject



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

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



104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/caboose/product.rb', line 104

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_variesObject



99
100
101
102
# File 'app/models/caboose/product.rb', line 99

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


121
122
123
# File 'app/models/caboose/product.rb', line 121

def related_items
  Array.new # TODO should be obvious
end

#toggle_category(cat_id, value) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/caboose/product.rb', line 155

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.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

#urlObject



117
118
119
# File 'app/models/caboose/product.rb', line 117

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