Class: Caboose::Variant
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Variant
- Defined in:
- app/models/caboose/variant.rb
Constant Summary collapse
- STATUS_ACTIVE =
'Active'
- STATUS_INACTIVE =
'Inactive'
- STATUS_DELETED =
'Deleted'
- SUBSCRIPTION_INTERVAL_MONTHLY =
'monthly'
- SUBSCRIPTION_INTERVAL_YEARLY =
'yearly'
- SUBSCRIPTION_PRORATE_METHOD_FLAT =
'flat'
- SUBSCRIPTION_PRORATE_METHOD_PERCENTAGE =
'percentage'
- SUBSCRIPTION_PRORATE_METHOD_CUSTOM =
'custom'
Class Method Summary collapse
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Instance Methods.
- #full_title ⇒ Object
- #on_sale? ⇒ Boolean
- #options ⇒ Object
- #product_image ⇒ Object
- #title ⇒ Object
Class Method Details
.find_by_options(product_id, option1 = nil, option2 = nil, option3 = nil) ⇒ Object
Class Methods
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/models/caboose/variant.rb', line 99 def self.(product_id, option1=nil, option2=nil, option3=nil) # Create the vars that will become the full conditions statement where = ['product_id=?'] values = [product_id.to_i] # Append option values if they exist if option1 where << 'option1=?' values << option1 end if option2 where << 'option2=?' values << option2 end if option3 where << 'option3=?' values << option3 end # Combine all the options into a single conditions statement conditions = [ where.join(' AND ') ].concat(values) # Return whatever is found return Variant.where(conditions).first end |
Instance Method Details
#as_json(options = {}) ⇒ Object
Instance Methods
133 134 135 136 137 138 139 140 |
# File 'app/models/caboose/variant.rb', line 133 def as_json(={}) self.attributes.merge({ :images => self.product_images.any? ? self.product_images : [self.product.product_images.first], :title => "#{self.product.title} (#{self..join(', ')})", :flat_rate_shipping_package => self.flat_rate_shipping_package, :flat_rate_shipping_method => self.flat_rate_shipping_method }) end |
#full_title ⇒ Object
146 147 148 149 |
# File 'app/models/caboose/variant.rb', line 146 def full_title return self.product.title if self..count == 0 return "#{self.product.title} - #{self.title}" end |
#on_sale? ⇒ Boolean
165 166 167 168 169 170 171 |
# File 'app/models/caboose/variant.rb', line 165 def on_sale? return false if self.sale_price.nil? || self.sale_price == 0.0 d = DateTime.now.utc return false if self.date_sale_starts && d < self.date_sale_starts return false if self.date_sale_ends && d > self.date_sale_ends return true end |
#options ⇒ Object
151 152 153 154 155 156 157 |
# File 'app/models/caboose/variant.rb', line 151 def arr = [] arr << self.option1 if self.option1 && self.option1.strip.length > 0 arr << self.option2 if self.option2 && self.option2.strip.length > 0 arr << self.option3 if self.option3 && self.option3.strip.length > 0 return arr end |
#product_image ⇒ Object
159 160 161 162 163 |
# File 'app/models/caboose/variant.rb', line 159 def product_image return self.product_images.first if self.product_images return self.product.product_images.first if self.product.product_images return nil end |
#title ⇒ Object
142 143 144 |
# File 'app/models/caboose/variant.rb', line 142 def title return self..join(' / ') end |