Permalink
Browse files

yay complexity, starting to implement the variants and groups features

  • Loading branch information...
1 parent 22362fb commit c456f05a145c2003e9b012914a77a7ab99f0d7a4 Dirk Kelly committed Sep 29, 2010
@@ -0,0 +1,43 @@
+class Admin::Shop::GroupsController < Admin::ResourceController
+ model_class ShopGroup
+
+ before_filter :config_global
+ before_filter :config_new, :only => [ :new, :create ]
+ before_filter :config_edit, :only => [ :edit, :update ]
+ before_filter :assets_global
+ before_filter :assets_index, :only => [ :index ]
+ before_filter :assets_edit, :only => [ :edit, :update ]
+
+ private
+
+ def config_global
+ @inputs ||= []
+ @meta ||= []
+ @buttons ||= []
+ @parts ||= []
+ @popups ||= []
+
+ @inputs << 'name'
+ @inputs << 'description'
+ end
+
+ def config_new
+ end
+
+ def config_edit
+ @parts << 'products'
+ @popups << 'available_products'
+ end
+
+ def assets_global
+ include_stylesheet 'admin/extensions/shop/edit'
+ include_stylesheet 'admin/extensions/shop/index'
+ end
+
+ def assets_index
+ end
+
+ def assets_edit
+ end
+
+end
View
@@ -3,4 +3,6 @@ class ShopGroup < ActiveRecord::Base
has_many :groupings, :class_name => 'ShopGrouping', :foreign_key => :group_id
has_many :products, :class_name => 'ShopProduct', :through => :groupings
+ validates_presence_of :name
+
end
@@ -13,6 +13,7 @@ class ShopProduct < ActiveRecord::Base
has_many :groupings, :class_name => 'ShopGrouping', :foreign_key => :product_id
has_many :groups, :class_name => 'ShopGroup', :through => :groupings, :uniq => true
has_many :related, :class_name => 'ShopProduct', :through => :groupings, :uniq => true
+ has_many :variants, :class_name => 'ShopProductVariant', :foreign_key => :product_id, :dependent => :destroy
before_validation :set_sku, :filter_sku
validates_presence_of :name, :sku, :category
@@ -0,0 +1,9 @@
+class ShopProductVariant < ActiveRecord::Base
+
+ belongs_to :product, :class_name => 'ShopProduct', :foreign_key => 'product_id'
+
+ validates_presence_of :product
+ validates_presence_of :name
+ validates_presence_of :price
+
+end
View
@@ -0,0 +1,20 @@
+class ShopVariant < ActiveRecord::Base
+
+ validates_presence_of :name
+ validates_uniqueness_of :name
+ validates_presence_of :options
+
+ def options
+ options = {}
+ if self.options_json.present?
+ result = ActiveSupport::JSON.decode(self.options_json)
+ result = Forms::Config.deep_symbolize_keys(options)
+ end
+ options
+ end
+
+ def options=(options)
+ self.options_json = ActiveSupport::JSON.encode(options)
+ end
+
+end
@@ -0,0 +1,11 @@
+class ShopGroupsChangeAddDescription < ActiveRecord::Migration
+ def self.up
+ add_column :shop_groups, :description, :text
+ add_column :pages, :shop_group_id, :integer
+ end
+
+ def self.down
+ remove_column :shop_groups, :description
+ remove_column :pages, :shop_group_id
+ end
+end
@@ -0,0 +1,20 @@
+class CreateVariants < ActiveRecord::Migration
+ def self.up
+ create_table :shop_variants do |t|
+ t.string :name
+ t.text :options_json
+ end
+
+ create_table :shop_product_variants do |t|
+ t.string :name
+ t.decimal :price
+
+ t.integer :product_id
+ end
+ end
+
+ def self.down
+ remove_table :shop_variants
+ remove_table :shop_product_variants
+ end
+end
View
@@ -4,16 +4,15 @@ class ShopExtension < Radiant::Extension
url "http://github.com/squaretalent/radiant-shop-extension"
extension_config do |config|
- config.gem 'activemerchant', :version => '1.7.3', :lib => 'active_merchant'
+ config.gem 'activemerchant', :version => '1.7.3', :lib => 'active_merchant'
config.gem 'will_paginate', :version => '2.3.14'
end
UserActionObserver.instance.send :add_observer!, ShopProduct
UserActionObserver.instance.send :add_observer!, ShopCategory
UserActionObserver.instance.send :add_observer!, ShopOrder
- def activate
-
+ def activate
# View Hooks
unless defined? admin.products
Radiant::AdminUI.send :include, Shop::Interface::Products, Shop::Interface::Customers, Shop::Interface::Orders
@@ -0,0 +1,15 @@
+class ShopGroupsDataset < Dataset::Base
+
+ uses :shop_products
+
+ def load
+ create_record :shop_groups, :breakfast,
+ :name => 'breakfast'
+
+ shop_groups(:breakfast).products = [
+ shop_products(:crusty_bread),
+ shop_products(:choc_milk)
+ ]
+ end
+
+end
@@ -1,4 +1,7 @@
-class ImagesDataset < Dataset::Base
+class ShopProductAttachmentsDataset < Dataset::Base
+
+ uses :shop_products
+
def load
images = [ :soft_bread_front, :soft_bread_back, :soft_bread_top, :crusty_bread_front, :warm_bread_front ]
@@ -8,6 +11,11 @@ def load
:asset_file_name => "#{image.to_s}_file_name.png",
:asset_content_type => "image/png",
:asset_file_size => i+1*1000
+
+ create_record :shop_product_attachment, image,
+ :image => images(image.to_sym),
+ :product => shop_products(image.to_s.split('_')[0,2].join('_').to_sym),
+ :position => i + 1
end
end
end
@@ -0,0 +1,17 @@
+class ShopProductVariantsDataset < Dataset::Base
+
+ uses :shop_products
+
+ def load
+ create_record :shop_product_variants, :mouldy_crusty_bread,
+ :name => 'mouldy',
+ :price => -2.50,
+ :product => shop_products(:crusty_bread)
+
+ create_record :shop_product_variants, :fresh_crusty_bread,
+ :name => 'fresh',
+ :price => 2.50,
+ :product => shop_products(:crusty_bread)
+ end
+
+end
@@ -1,6 +1,6 @@
class ShopProductsDataset < Dataset::Base
- uses :shop_categories, :images
+ uses :shop_categories
def load
categories = {
@@ -20,16 +20,6 @@ def load
:category => shop_categories(category)
end
end
-
- shop_products(:soft_bread).images = [
- images(:soft_bread_front),
- images(:soft_bread_back),
- images(:soft_bread_top)
- ]
-
- shop_products(:crusty_bread).images << images(:crusty_bread_front)
-
- shop_products(:warm_bread).images << images(:warm_bread_front)
end
end
@@ -0,0 +1,13 @@
+class ShopVariantsDataset < Dataset::Base
+
+ def load
+ create_record :shop_variants, :bread_states,
+ :name => 'bread states',
+ :options_json => ActiveSupport::JSON.encode([ 'mouldy', 'fresh' ])
+
+ create_record :shop_variants, :milk_states,
+ :name => 'milk states',
+ :options_json => ActiveSupport::JSON.encode([ 'cold', 'warm', 'sour' ])
+ end
+
+end
@@ -1,20 +1,20 @@
require 'spec/spec_helper'
#
-# Tests for shop order page model extensions
+# Tests for image model extensions
#
describe Shop::Models::Image do
- dataset :images, :shop_products
+ dataset :shop_products, :shop_product_attachments
before :each do
@image = images(:soft_bread_front)
end
context 'relationships' do
- describe 'belongs_to :shop_category' do
- it 'should accept and return a shop_category object' do
+ describe 'product' do
+ it 'should accept and return a product object' do
@shop_product = shop_products(:soft_bread)
@image.shop_products << @shop_product
@@ -2,7 +2,7 @@
describe Shop::Tags::Product do
- dataset :pages, :shop_products
+ dataset :pages, :shop_products, :shop_product_attachments
it 'should describe these tags' do
Shop::Tags::Product.tags.sort.should == [
@@ -0,0 +1,29 @@
+require 'spec/spec_helper'
+
+describe ShopGroup do
+
+ dataset :shop_groups
+
+ context 'associations' do
+ before :each do
+ @group = shop_groups(:breakfast)
+ end
+
+ describe 'products' do
+ before :each do
+ @product = shop_products(:crusty_bread)
+ end
+
+ it 'should contain an array of products' do
+ @group.products.include?(@product).should === true
+ end
+
+ it 'should accept new products' do
+ @product = shop_products(:choc_milk)
+ @group.products << @product
+ @group.products.include?(@product).should === true
+ end
+ end
+ end
+
+end
@@ -1,7 +1,8 @@
require 'spec/spec_helper'
describe ShopProductAttachment do
- dataset :shop_products
+
+ dataset :shop_products, :shop_product_attachments
context 'attributes' do
@@ -86,7 +86,6 @@
it 'should have an array of images' do
@product.attachments.class.should == Array
@product.images.class.should == Array
- @product.images.length.should === 3
@product.images.length.should === @product.attachments.length
end
@@ -0,0 +1,44 @@
+require 'spec/spec_helper'
+
+describe ShopProductVariant do
+
+ dataset :shop_products, :shop_product_variants
+
+ describe 'relationships' do
+ before :each do
+ @product = shop_products(:crusty_bread)
+ end
+
+ context 'product' do
+ it 'should belong to one' do
+ @product_variant = shop_product_variants(:mouldy_crusty_bread)
+ @product_variant.product.should === @product
+ end
+ end
+ end
+
+ describe 'validations' do
+ before :each do
+ @product_variant = shop_product_variants(:mouldy_crusty_bread)
+ end
+ context 'product' do
+ it 'should require' do
+ @product_variant.product = nil
+ @product_variant.valid?.should === false
+ end
+ end
+ context 'name' do
+ it 'should require' do
+ @product_variant.name = nil
+ @product_variant.valid?.should === false
+ end
+ end
+ context 'price' do
+ it 'should require' do
+ @product_variant.price = nil
+ @product_variant.valid?.should === false
+ end
+ end
+ end
+
+end
Oops, something went wrong.

0 comments on commit c456f05

Please sign in to comment.