piggybak_variants 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -19,6 +19,8 @@ In the admin, define option configurations and option values for each option, th
19
19
 
20
20
  Finally, add `<%= variant_cart_form(@instance) %>` to your sellable item's show page to render the cart form.
21
21
 
22
+ OPTION: `<%= variant_cart_form(@instance, :controls => 'dropdowns) %>` to render dropdowns instead of radio buttons.
23
+
22
24
 
23
25
  TODO
24
26
  ========
@@ -1,32 +1,67 @@
1
- $(function() {
2
- $('.variant_options input[type=radio]').click(function() { piggybak_variants.toggle_variant_form(); });
3
- piggybak_variants.toggle_variant_form();
4
- });
1
+ function refreshControls(all_selected, selected_key){
2
+ if(all_selected && variant_map[selected_key]) {
3
+ $('.variant_options form').show();
4
+ $('#sellable_id').val(variant_map[selected_key].id);
5
+ $('#variant_price span').html(variant_map[selected_key].price);
6
+ } else if(all_selected) { // if all selected and not available
7
+ $('.variant_options form').hide();
8
+ $('.unavailable').show();
9
+ } else { // if not all selected
10
+ $('.all_selected').show();
11
+ $('.variant_options form').hide();
12
+ }
13
+ };
5
14
 
6
- var piggybak_variants = {
7
- toggle_variant_form: function() {
8
- $('.unavailable, .all_selected').hide();
9
- var all_selected = true;
10
- var selected = new Array();
11
- $.each($('.option'), function(i, j) {
12
- var option_id = $(j).attr('id');
13
- if($('input[name=' + option_id + ']:checked').length) {
14
- selected.push($('input[name=' + option_id + ']:checked').val());
15
- } else {
16
- all_selected = false;
17
- }
18
- });
19
- var selected_key = selected.join('_');
20
- if(all_selected && variant_map[selected_key]) {
21
- $('.variant_options form').show();
22
- $('#sellable_id').val(variant_map[selected_key].id);
23
- $('#variant_price span').html(variant_map[selected_key].price);
24
- } else if(all_selected) { // if all selected and not available
25
- $('.variant_options form').hide();
26
- $('.unavailable').show();
27
- } else { // if not all selected
28
- $('.all_selected').show();
29
- $('.variant_options form').hide();
15
+ function extractRadioButtonValues(){
16
+ var all_selected = true;
17
+ var selected = new Array();
18
+ $.each($('.option'), function(i, j) {
19
+ var option_id = $(j).attr('id');
20
+ if($('input[name=' + option_id + ']:checked').length) {
21
+ selected.push($('input[name=' + option_id + ']:checked').val());
22
+ } else {
23
+ all_selected = false;
30
24
  }
31
- }
25
+ });
26
+ return {all_selected: all_selected, selected: selected};
27
+ };
28
+
29
+ function extractDropdownValues(){
30
+ var all_selected = true;
31
+ $('.variant_options select').each(function() {
32
+ if ($(this).attr('value') == '') {
33
+ all_selected = false;
34
+ }
35
+ });
36
+ var selected = new Array();
37
+ $.each($('.option'), function(i, j) {
38
+ var option_id = $(j).attr('id');
39
+ var select_id = $(j).children("select").attr('id');
40
+ if($('select#' + select_id + ' option:selected')) {
41
+ selected.push($('select#' + select_id + ' option:selected').val());
42
+ } else {
43
+ all_selected = false;
44
+ }
45
+ });
46
+ return {all_selected: all_selected, selected: selected};
47
+ };
48
+
49
+ function toggleVariantForm(){
50
+ $('.unavailable, .all_selected').hide();
51
+ var selected_values = {};
52
+ if ($('input[type=radio]').length > 0){
53
+ selected_values = extractRadioButtonValues();
54
+ }
55
+ else{
56
+ selected_values = extractDropdownValues();
57
+ };
58
+ var selected_key = selected_values['selected'].sort().join('_');
59
+ refreshControls(selected_values['all_selected'], selected_key);
32
60
  };
61
+
62
+ $(function() {
63
+ $('.variant_options input[type=radio]').click(function() { toggleVariantForm(); });
64
+ $('.variant_options select').change(function() { toggleVariantForm(); });
65
+ toggleVariantForm();
66
+ });
67
+
@@ -1,6 +1,6 @@
1
1
  module PiggybakVariantsHelper
2
- def variant_cart_form(object)
3
- render "piggybak_variants/cart/form", :object => object
2
+ def variant_cart_form(object, options = {:controls => 'radio_buttons'})
3
+ render "piggybak_variants/cart/#{options[:controls]}_form", :object => object
4
4
  end
5
5
 
6
6
  def options_for_klass(klass)
@@ -0,0 +1,26 @@
1
+ <% if object.reflections.keys.include?(:variants) -%>
2
+ <%= javascript_include_tag "piggybak_variants/piggybak_variants" %>
3
+
4
+ <script type="text/javascript">
5
+ var variant_map = <%= raw variant_map(object) %>;
6
+ </script>
7
+
8
+ <div class="variant_options">
9
+ <% options_for_klass(object.class).each do |option| -%>
10
+ <div class="option" id="option_<%= option.id %>">
11
+ <h4><%= option.name %></h4>
12
+ <%= select_tag option.name, options_for_select(option.option_values.map{ |o| [o.name, o.id, { name: "option_#{option.name}-#{o.id}"}] }), include_blank: true %>
13
+ <br />
14
+ </div>
15
+ <% end -%>
16
+ <span class="all_selected">You must select a choice from each option.</span>
17
+ <span class="unavailable">This combination is not available.</span>
18
+ <%= form_tag piggybak.cart_add_url do -%>
19
+ <input type="hidden" id="sellable_id" name="sellable_id" />
20
+ <!-- <div id="variant_price">Price: <span></span></div> -->
21
+ <label for="quantity">Quantity</label>
22
+ <%= text_field_tag :quantity, 1 %>
23
+ <%= submit_tag "Add to Cart", :id => "submit" %>
24
+ <% end -%>
25
+ </div>
26
+ <% end -%>
@@ -1,3 +1,3 @@
1
1
  module PiggybakVariants
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piggybak_variants
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-27 00:00:00.000000000 Z
12
+ date: 2013-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -43,7 +43,8 @@ files:
43
43
  - app/models/piggybak_variants/option_value.rb
44
44
  - app/helpers/piggybak_variants_helper.rb
45
45
  - app/views/rails_admin/main/_option_values.html.haml
46
- - app/views/piggybak_variants/cart/_form.html.erb
46
+ - app/views/piggybak_variants/cart/_dropdowns_form.html.erb
47
+ - app/views/piggybak_variants/cart/_radio_buttons_form.html.erb
47
48
  - config/routes.rb
48
49
  - db/migrate/20121016150359_create_options.rb
49
50
  - db/migrate/20121022205428_create_option_configurations.rb
@@ -103,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
104
  version: '0'
104
105
  segments:
105
106
  - 0
106
- hash: 2470368963455998095
107
+ hash: -1786782223975916753
107
108
  required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  none: false
109
110
  requirements:
@@ -112,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
113
  version: '0'
113
114
  segments:
114
115
  - 0
115
- hash: 2470368963455998095
116
+ hash: -1786782223975916753
116
117
  requirements: []
117
118
  rubyforge_project:
118
119
  rubygems_version: 1.8.23