Skip to content

Commit

Permalink
product type management
Browse files Browse the repository at this point in the history
  • Loading branch information
vala committed Feb 15, 2013
1 parent 64fa8ec commit c6fd5cf
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 6 deletions.
37 changes: 31 additions & 6 deletions app/models/glysellin/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Product < ActiveRecord::Base
# Products can belong to a brand
belongs_to :brand, :inverse_of => :products

belongs_to :product_type

has_many :variants, class_name: 'Glysellin::Variant',
before_add: :set_product_on_variant

Expand All @@ -42,7 +44,7 @@ class Product < ActiveRecord::Base
attr_accessible :description, :name, :sku, :slug, :vat_rate,
:brand, :taxonomies, :images, :published,
:display_priority, :images_attributes, :taxonomy_ids, :unlimited_stock,
:position, :brand_id, :variants_attributes, :variants
:position, :brand_id, :variants_attributes, :variants, :product_type_id

# :bundled_products_attributes

Expand All @@ -59,20 +61,43 @@ class Product < ActiveRecord::Base

# Callbacks
#
before_validation :set_slug, :set_sku, :set_vat_rate, :ensure_variant

# We always check we have a slug for our product
def set_slug
self.slug = name.to_slug
end

# And as for the validation, if the SKU is configured to be autoset,
# we check generate it
before_validation do
self.slug = self.name.to_slug
unless (self.sku && self.sku.length > 0) || !Glysellin.autoset_sku
self.sku = self.generate_sku
def set_sku
unless (sku && sku.length > 0) || !Glysellin.autoset_sku
self.sku = generate_sku
end
end

if !self.vat_rate
def set_vat_rate
if !vat_rate
self.vat_rate = Glysellin.default_vat_rate
end
end

def ensure_variant
variants.build(product: self) unless variants.length > 0

if product_type
variants.each do |variant|
product_type.property_types.each do |type|
unless variant.properties.send(type.name)
variant.properties.build(type_id: type.id, variant: variant)
end
end
end
end
end

scope :published, where('glysellin_products.published = ?', true)

class << self
# Find products with taxonomy slugs or taxonomies
#
Expand Down
3 changes: 3 additions & 0 deletions app/models/glysellin/product_property_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ class ProductPropertyType < ActiveRecord::Base
attr_accessible :eot_price, :in_stock, :name, :position, :price, :published, :sku, :slug, :unlimited_stock

has_many :properties, class_name: "Glysellin::ProductProperty"

has_and_belongs_to_many :product_types, class_name: 'Glysellin::ProductType',
join_table: 'glysellin_product_types_property_types'
end
end
11 changes: 11 additions & 0 deletions app/models/glysellin/product_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Glysellin::ProductType < ActiveRecord::Base
self.table_name = 'glysellin_product_types'

has_and_belongs_to_many :property_types,
class_name: 'Glysellin::ProductPropertyType',
join_table: 'glysellin_product_types_property_types'

has_many :products

attr_accessible :name, :product_ids, :property_type_ids
end
10 changes: 10 additions & 0 deletions app/models/glysellin/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class Variant < ActiveRecord::Base

before_validation :check_prices

after_initialize :prepare_properties

def prepare_properties
if product && product.product_type
product.product_type.property_types.each do |type|
properties.build(type: type) unless properties.send(type)
end
end
end

def description
product.description
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20130214184204_create_glysellin_product_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateGlysellinProductTypes < ActiveRecord::Migration
def change
create_table :glysellin_product_types do |t|
t.string :name

t.timestamps
end
end
end
15 changes: 15 additions & 0 deletions db/migrate/20130214184352_create_product_type_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateProductTypeBehavior < ActiveRecord::Migration
def up
create_table :glysellin_product_types_properties, id: false do |t|
t.references :product_type, :product_property
end

add_column :glysellin_products, :product_type_id, :integer
end

def down
remove_column :glysellin_products, :product_type_id

drop_table :glysellin_product_types_properties
end
end
7 changes: 7 additions & 0 deletions test/fixtures/glysellin/product_types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
name: MyString

two:
name: MyString
7 changes: 7 additions & 0 deletions test/unit/glysellin/product_type_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Glysellin::ProductTypeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit c6fd5cf

Please sign in to comment.