piggybak_variants 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -19,8 +19,13 @@ 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.
22
+ OPTION: `<%= variant_cart_form(@instance, :controls => 'dropdowns') %>` to render dropdowns instead of radio buttons.
23
23
 
24
+ In your `config/environments/production.rb`, add the following line to allow pre-compilation of piggybak_variants.js:
25
+
26
+ config.assets.precompile += %w(piggybak_variants/piggybak_variants.js)
27
+
28
+ So either add that line, or if you already have it enabled just add `piggybak_variants/piggybak_variants.js` to the array of values.
24
29
 
25
30
  TODO
26
31
  ========
@@ -1,67 +1,66 @@
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
- };
14
-
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;
1
+ var piggybak_variants = {
2
+ refreshControls : function(data) {
3
+ if(data.all_selected) { // if all selected
4
+ var selected_key = data.selected.sort().join('_');
5
+ if(variant_map[selected_key]) {
6
+ $('.variant_options form').show();
7
+ $('#sellable_id').val(variant_map[selected_key].id);
8
+ $('#variant_price span').html(variant_map[selected_key].price);
9
+ } else { // if variant not available
10
+ $('.variant_options form').hide();
11
+ $('.unavailable').show();
12
+ }
13
+ } else { // if not all selected
14
+ $('.all_selected').show();
15
+ $('.variant_options form').hide();
24
16
  }
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);
60
- };
17
+ },
18
+ extractRadioButtonValues: function() {
19
+ var all_selected = true;
20
+ var selected = new Array();
21
+ $.each($('.option'), function(i, j) {
22
+ var option_id = $(j).attr('id');
23
+ if($('input[name=' + option_id + ']:checked').length) {
24
+ selected.push($('input[name=' + option_id + ']:checked').val());
25
+ } else {
26
+ all_selected = false;
27
+ }
28
+ });
29
+ return { all_selected: all_selected, selected: selected };
30
+ },
31
+ extractDropdownValues: function() {
32
+ var all_selected = true;
33
+ // TODO: Nice to see if easier way to select by value, but select[value=""] doesn't work
34
+ $('.variant_options select').each(function() {
35
+ if ($(this).val() == '') {
36
+ all_selected = false;
37
+ }
38
+ });
39
+ if(!all_selected) {
40
+ return { all_selected: false, selected: '' };
41
+ }
42
+
43
+ var selected = new Array();
44
+ $.each($('.variant_options .option select'), function(i, j) {
45
+ selected.push($(this).val());
46
+ });
47
+ return { all_selected: true, selected: selected};
48
+ },
49
+ toggleVariantForm : function() {
50
+ $('.unavailable, .all_selected').hide();
51
+ var data;
52
+ if ($('.variant_options input[type=radio]').length > 0){
53
+ data = piggybak_variants.extractRadioButtonValues();
54
+ } else {
55
+ data = piggybak_variants.extractDropdownValues();
56
+ };
57
+ piggybak_variants.refreshControls(data);
58
+ }
59
+ }
61
60
 
62
61
  $(function() {
63
- $('.variant_options input[type=radio]').click(function() { toggleVariantForm(); });
64
- $('.variant_options select').change(function() { toggleVariantForm(); });
65
- toggleVariantForm();
62
+ $('.variant_options input[type=radio]').click(function() { piggybak_variants.toggleVariantForm(); });
63
+ $('.variant_options select').change(function() { piggybak_variants.toggleVariantForm(); });
64
+ piggybak_variants.toggleVariantForm();
66
65
  });
67
66
 
@@ -9,7 +9,7 @@
9
9
  <% options_for_klass(object.class).each do |option| -%>
10
10
  <div class="option" id="option_<%= option.id %>">
11
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 %>
12
+ <%= select_tag option.name, options_for_select(option.option_values.map{ |o| [o.name, o.id] }), include_blank: true %>
13
13
  <br />
14
14
  </div>
15
15
  <% end -%>
@@ -17,10 +17,10 @@
17
17
  <span class="unavailable">This combination is not available.</span>
18
18
  <%= form_tag piggybak.cart_add_url do -%>
19
19
  <input type="hidden" id="sellable_id" name="sellable_id" />
20
- <!-- <div id="variant_price">Price: <span></span></div> -->
20
+ <div id="variant_price">Price: <span></span></div>
21
21
  <label for="quantity">Quantity</label>
22
22
  <%= text_field_tag :quantity, 1 %>
23
23
  <%= submit_tag "Add to Cart", :id => "submit" %>
24
24
  <% end -%>
25
25
  </div>
26
- <% end -%>
26
+ <% end -%>
@@ -1,3 +1,3 @@
1
1
  module PiggybakVariants
2
- VERSION = "0.0.14"
2
+ VERSION = "0.0.15"
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.14
4
+ version: 0.0.15
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-03-22 00:00:00.000000000 Z
12
+ date: 2013-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  segments:
106
106
  - 0
107
- hash: -1786782223975916753
107
+ hash: 4072402557057022978
108
108
  required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  none: false
110
110
  requirements:
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  version: '0'
114
114
  segments:
115
115
  - 0
116
- hash: -1786782223975916753
116
+ hash: 4072402557057022978
117
117
  requirements: []
118
118
  rubyforge_project:
119
119
  rubygems_version: 1.8.23