Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jsqu99 committed Mar 1, 2011
1 parent fa0dec3 commit 54ef9d6
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\#*
*~
.#*
.DS_Store
.idea
.project
tmp
nbproject
*.swp
doc/notes.txt
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Rails Dog LLC nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SpreeOnDemandVariants
=====================

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2011 [name of extension creator], released under the New BSD License
31 changes: 31 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require File.expand_path('../../config/application', __FILE__)

require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rake/packagetask'
require 'rake/gempackagetask'

spec = eval(File.read('spree_on_demand_variants.gemspec'))

Rake::GemPackageTask.new(spec) do |p|
p.gem_spec = spec
end

desc "Release to gemcutter"
task :release => :package do
require 'rake/gemcutter'
Rake::Gemcutter::Tasks.new(spec).define
Rake::Task['gem:push'].invoke
end

desc "Default Task"
task :default => [ :spec ]

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new

# require 'cucumber/rake/task'
# Cucumber::Rake::Task.new do |t|
# t.cucumber_opts = %w{--format pretty}
# end
4 changes: 4 additions & 0 deletions app/controllers/admin/option_values_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Admin::OptionValuesController < Admin::BaseController
resource_controller
belongs_to :option_types # what does this do?
end
43 changes: 43 additions & 0 deletions app/controllers/products_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ProductsController.class_eval do
# before_filter :load_option_types_for_on_demand_variants, :only => :show
#
# def load_option_types_for_on_demand_variants
# if @product.has_on_demand_variants?
# @option_types= @product.option_types
# end
# end


# Adds a new item to the order (creating a new order if none already exists)
#
# Parameters can be passed using the following possible parameter configurations:
#
# * Single variant/quantity pairing
# +:variants => {variant_id => quantity}+
#
# * Multiple products at once
# +:products => {product_id => variant_id, product_id => variant_id}, :quantity => quantity +
# +:products => {product_id => variant_id, product_id => variant_id}}, :quantity => {variant_id => quantity, variant_id => quantity}+
def populate
@order = current_order(true)

params[:products].each do |product_id,variant_id|
quantity = params[:quantity].to_i if !params[:quantity].is_a?(Hash)
quantity = params[:quantity][variant_id].to_i if params[:quantity].is_a?(Hash)
@order.add_variant(Variant.find(variant_id), quantity) if quantity > 0
end if params[:products]

params[:variants].each do |variant_id, quantity|
quantity = quantity.to_i
@order.add_variant(Variant.find(variant_id), quantity) if quantity > 0
end if params[:variants]

# need to see what the parms look like before going any further
# params[:option_values].each do |option_value_id, quantity|
# quantity = quantity.to_i
# @order.add_variant(Variant.find(variant_id), quantity) if quantity > 0
# end if params[:variants]

redirect_to cart_path
end
end
5 changes: 5 additions & 0 deletions app/models/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Product.class_eval do
def has_on_demand_variants?
has_on_demand_variants
end
end
41 changes: 41 additions & 0 deletions app/views/admin/option_values/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<div class="yui-gb">
<div class="yui-u first">

<!-- each option type -->
<% @product.options.each do |option| %>

<h2><%= option.presentation %></h2>

<h3>What price modifier strategy does this <%= option.presentation %> use?</h3>

<%= f.fields_for(option) do |option_fields| %>
<p><%= option_fields.label :price_modifier_type, t("price_modifier_type") %>:<br />
<%= option_fields.select(:price_modifier_type, options_for_select([["none",t("none")],["single",t("single")],["multiple", t("multiple")]]) %>
<!-- TODO?: options_from_collection_for_select(@price_modifier_types, 'id', 'name') -->
</p>
<% end %> <!-- fields for -->

<table class="index">
<tr>
<th><%= t("option_values") %></th>
<th><%= t("price") %></th>
</tr>

<!-- each option value -->
<% option.option_type.option_values.each do |ov| %>
<% option_fields.fields_for(ov) do |ov_fields| %>
<tr id="<%= dom_id(ov) %>" %>
<td><%= ov.presentation %></td>
<td><%= ov_fields.label :price_modifier, t("price_modifier") %>:<br />
<%= ov_fields.text_field :price_modifier %>
</td>
</tr>
<% end %> <!-- ov fields_for -->
<% end %> <!-- each -->
<% end %> <!-- option fields_for -->
<% end %> <!-- each -->

</table>
</div>
</div>
13 changes: 13 additions & 0 deletions app/views/admin/option_values/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= render :partial => 'admin/shared/product_sub_menu' %>
<%= render :partial => 'admin/shared/product_tabs', :locals => {:current => "Variants"} %>
<%= render "shared/error_messages", :target => @variant %>
<%= form_for(@variant, :url => object_url, :html => { :method => :put }) do |f| %>
<%= hook :admin_variant_edit_form, {:f => f} do %>
<%= render :partial => "form", :locals => { :f => f } %>
<% end %>
<%= render :partial => 'admin/shared/edit_resource_links' %>
<% end %>
57 changes: 57 additions & 0 deletions app/views/admin/option_values/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%= render :partial => 'admin/shared/product_sub_menu' %>
<%= render :partial => 'admin/shared/product_tabs', :locals => {:current => "Variants"} %>


<!-- TODO: here we _should_ show each option value that has been set up as an on-demand option value -->
<!-- <table class="index">
<tr>
<th><%= t("options") %></th>
<th><%= t("price") %></th>
<th><%= t("sku") %></th>
<% Variant.additional_fields.select{|f| f[:only].nil? || f[:only].include?(:variant) }.each do |field| %>
<th><%= t("activerecord.attributes.variant." + field[:name].downcase, :default => field[:name].titleize) %></th>
<% end %>
<th><%= t("on_hand") %></th>
<th><%= t("action") %></th>
</tr>
<% @variants.each do |variant| %>
-->
<!-- you can skip variant with no options: that's just the default variant that all products have -->
<!--
<% next if variant.option_values.empty? %>
<tr id="<%= dom_id(variant) %>" <%= 'style="color:red;"' if variant.deleted? %>>
<td><%= variant_options variant %></td>
<td><%= variant.price %></td>
<td><%= variant.sku %></td>
<% Variant.additional_fields.select{|f| f[:only].nil? || f[:only].include?(:variant) }.each do |field| %>
<td><%= variant[field[:name].gsub(" ", "_").downcase] %></td>
<% end %>
<td><%= variant.on_hand %></td>
<td valign="top">
<%= link_to_edit(variant) unless variant.deleted? %>
&nbsp;
<%= link_to_delete(variant) unless variant.deleted? %>
</td>
</tr>
<% end %>
<% unless @product.has_variants? %>
<tr><td colspan="9"><%= t("none") %></td></tr>
<% end %>
</table>
-->
<% if @product.options.empty? %>
<p>
<%= t("to_add_variants_you_must_first_define") %> <%= link_to t("option_types"), selected_admin_product_option_types_url(@product) %>
</p>
<% else %>
<div id="new_variant"></div>
<br/>
<p id="new_var_link">
<%= link_to icon('add') + ' ' + t("new_variant"), new_admin_product_variant_url(@product), :remote => :true, 'data-update' => 'new_variant', :class => 'iconlink' %>
&nbsp;|&nbsp;<%= link_to @deleted.blank? ? t("show_deleted") : t("show_active"), admin_product_variants_url(@product, :deleted => @deleted.blank? ? "on" : "off") %>
</p>
<%= image_tag "spinner.gif", :plugin=>"spree", :style => "display:none", :id => 'busy_indicator' %>
<% end %>
9 changes: 9 additions & 0 deletions app/views/admin/option_values/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= render "shared/error_messages", :target => @variant %>
<%= form_for(@product, :url => collection_url) do |f| %>
<fieldset>
<%= render :partial => "form", :locals => { :f => f } %>
<%= render :partial => 'admin/shared/new_resource_links' %>
</fieldset>
<% end %>
5 changes: 5 additions & 0 deletions app/views/admin/products/_has_on_demand_variants.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>
<%= f.label :has_on_demand_variants, t("has_on_demand_variants")%><br />
<%= f.check_box :has_on_demand_variants %>
</p>

1 change: 1 addition & 0 deletions app/views/admin/products/_option_values_tab.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= tab(:option_types, :match_path => '/option_values') %>
61 changes: 61 additions & 0 deletions app/views/products/_cart_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<%= form_for :order, :url => populate_orders_url do |f| %>
<%= hook :inside_product_cart_form do %>
<% if product_price(@product) %>
<%= hook :product_price do %>
<p class="prices">
<%= t("price") %>
<br />
<span class="price selling"><%= product_price(@product) %></span>
</p>
<% end %>
<% end %>
<% if @product.has_on_demand_variants?
@product.option_types.each do |ot|
%>
<%= f.label ot.name, ot.name %>
<% option_values=OptionValue.where(:option_type_id => ot.id) %>
<%= select_tag ot.name, options_from_collection_for_select(option_values, "id", "presentation"), :class => 'on-demand-option-select' %>
<% end %>
<% elsif @product.has_variants? %>
<div id="product-variants">
<h2><%= t('variants') %></h2>
<ul>
<% has_checked = false
@product.variants.active.each_with_index do |v,index|
next if v.option_values.empty? || (!v.in_stock && !Spree::Config[:show_zero_stock_products])
checked = !has_checked && (v.in_stock || Spree::Config[:allow_backorders])
has_checked = true if checked %>
<li>
<label>
<%= radio_button_tag "products[#{@product.id}]", v.id, checked, :disabled => !v.in_stock && !Spree::Config[:allow_backorders] %>
<span class="variant-description">
<%= variant_options v %>
</span>
<% if variant_price_diff v %>
<span class="price diff"><%= variant_price_diff v %></span>
<% end %>
</label>
</li>
<% end%>
</ul>
</div>
<% end%>
<% if @product.has_stock? || Spree::Config[:allow_backorders] %>
<%= text_field_tag @product.has_variants? ? :quantity : "variants[#{@product.master.id}]"),
1, :class => "title", :size => 3 %>
&nbsp;
<button type='submit' class='large primary'>
<%= image_tag('/images/add-to-cart.png') + t('add_to_cart') %>
</button>
<% else %>
<%= content_tag('strong', t('out_of_stock')) %>
<% end %>
<% end %>
<% end %>
<% content_for :head do %>
<%= javascript_include_tag 'product' %>
<% end %>
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
en:
has_on_demand_variants: "Has on-demand variants?"
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Rails.application.routes.draw do
namespace :admin do
resources :products do
resources :option_values
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddDynamicVariantsFlagToProducts < ActiveRecord::Migration
def self.up
add_column :products, :has_on_demand_variants, :boolean, :default => false
end

def self.down
remove_column :products, :has_on_demand_variants
end
end
11 changes: 11 additions & 0 deletions db/migrate/20110225135642_add_price_modifiers_to_option_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddPriceModifiersToOptionValues < ActiveRecord::Migration
def self.up
add_column :product_option_types, :price_modifier_type, :string, :null => true, :default => nil
add_column :option_values, :price_modifier, :decimal, :null => true, :default => nil, :precision => 8, :scale => 2
end

def self.down
remove_column :product_option_types, :price_modifier_type
remove_column :option_values, :price_modifier
end
end
17 changes: 17 additions & 0 deletions lib/spree_on_demand_variants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spree_core'
require 'spree_on_demand_variants_hooks'

module SpreeOnDemandVariants
class Engine < Rails::Engine

config.autoload_paths += %W(#{config.root}/lib)

def self.activate
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.env.production? ? require(c) : load(c)
end
end

config.to_prepare &method(:activate).to_proc
end
end
4 changes: 4 additions & 0 deletions lib/spree_on_demand_variants_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class SpreeOnDemandVariantsHooks < Spree::ThemeSupport::HookListener
insert_after :admin_product_form_additional_fields, 'admin/products/has_on_demand_variants'
insert_after :admin_product_sub_tabs, 'admin/products/option_values_tab'
end
Loading

0 comments on commit 54ef9d6

Please sign in to comment.