effective_addresses 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 040257bf768402e6145101e420df24f063c5a8c3
4
- data.tar.gz: 0ee1ac756cf2119af9408f88517b5e630e361316
3
+ metadata.gz: 0028a63a8a2e844fcb596934bad4be1510a2ca9b
4
+ data.tar.gz: 447d7c0e5e7f94e5d6eb7c474bb108eda0c1a423
5
5
  SHA512:
6
- metadata.gz: ffcafceb79f86ecd54d39123eee62280af339dffb3103c35b8df8e9573130c5169894bb1e2f417908cbbadaeaaf8c5853d7f24239edcbb76fb8e45f52b5187bd
7
- data.tar.gz: 44442cbfb9cee3de8ac731bb212a9a940609c364578f03bee7f5049e184e1d5e272e69fbe1e4e8f55c7063c3b57b17193ba348d04906715c13385a0affc98173
6
+ metadata.gz: 7bf04d81bec30b72a36d8f2b28c234788ecd9164cfc05765f00a967ca12329862e4794d1daa3e9ffa14df31d2cfb523b74153572b72758a92eb2d4621fa7f12b
7
+ data.tar.gz: f2cc90da3d12a86b05fe723194392cf7c63a893e5f1348e0e66cff7cf4be11b6e655f8e691ab3bb7cfdbaaccd38951cd529c0830fdc8c69a212c4a59b71f0b2c
@@ -0,0 +1,18 @@
1
+ $(document).on 'change', "input[name$='[shipping_address][shipping_address_same_as_billing]']", (event) ->
2
+ $obj = $(event.currentTarget)
3
+
4
+ shipping_fields = $obj.closest('form').find("
5
+ div[class*='_shipping_address_address1'],
6
+ div[class*='_shipping_address_address2'],
7
+ div[class*='_shipping_address_city'],
8
+ div[class*='_shipping_address_country_code'],
9
+ div[class*='_shipping_address_state_code'],
10
+ div[class*='_shipping_address_postal_code']
11
+ ")
12
+
13
+ if $obj.is(':checked')
14
+ shipping_fields.hide().find('input,select').prop('disabled', true)
15
+ else
16
+ shipping_fields.show().find('input,select').removeAttr('disabled')
17
+
18
+
@@ -11,52 +11,62 @@ module ActsAsAddressable
11
11
  end
12
12
 
13
13
  included do
14
- has_many :addresses, :as => :addressable, :class_name => 'Effective::Address', :dependent => :delete_all, :autosave => true
14
+ has_many :addresses, as: :addressable, class_name: 'Effective::Address', dependent: :delete_all, autosave: true
15
15
 
16
- # Setup validations and methods
16
+ # Normalize categories
17
17
  categories = @acts_as_addressable_opts.try(:flatten) || []
18
18
 
19
- if categories.first.kind_of?(Hash) # We were passed some validation requirements
20
- categories = categories.first
21
-
22
- categories.each do |category, options| # billing, shipping
23
- category = category.to_s.gsub('_address', '')
24
-
25
- self.send(:define_method, "#{category}_address") { effective_address(category) }
26
- self.send(:define_method, "#{category}_addresses") { effective_addresses(category) }
27
-
28
- validates "#{category}_address", :effective_address_valid => true
19
+ if categories.first.kind_of?(Hash)
20
+ categories = categories.first.transform_keys { |category| category.to_s.gsub('_address', '') }
21
+ else
22
+ categories = categories.map { |category| category.to_s.gsub('_address', '') }
23
+ end
29
24
 
30
- if options.kind_of?(Hash)
31
- validates "#{category}_address", :presence => options.fetch(:presence, false)
32
- validates "#{category}_address", :effective_address_full_name_presence => options.fetch(:use_full_name, EffectiveAddresses.use_full_name)
25
+ # Categories can be either:
26
+ # {'billing'=>{:singular=>true, :use_full_name=>false}, 'shipping'=>{:singular=>true, :use_full_name=>false}}
27
+ # or
28
+ # {'billing' => true, 'shipping' => true}
29
+ # or
30
+ # ['billing', 'shipping']
31
+
32
+ categories.each do |category, options = {}|
33
+ # Define our two getter methods
34
+ self.send(:define_method, "#{category}_address") { effective_address(category) }
35
+ self.send(:define_method, "#{category}_addresses") { effective_addresses(category) }
36
+
37
+ # Define our setter method
38
+ if options.kind_of?(Hash) && options[:singular]
39
+ self.send(:define_method, "#{category}_address=") { |atts| set_singular_effective_address(category, atts) }
40
+ else
41
+ self.send(:define_method, "#{category}_address=") { |atts| set_effective_address(category, atts) }
42
+ end
33
43
 
34
- if options.fetch(:singular, false)
35
- self.send(:define_method, "#{category}_address=") { |atts| set_singular_effective_address(category, atts) }
36
- else
37
- self.send(:define_method, "#{category}_address=") { |atts| set_effective_address(category, atts) }
38
- end
44
+ # Consider validations now
45
+ validates "#{category}_address", effective_address_valid: true
46
+
47
+ case options
48
+ when Hash
49
+ validates "#{category}_address", presence: options.fetch(:presence, false)
50
+ validates "#{category}_address", effective_address_full_name_presence: options.fetch(:use_full_name, EffectiveAddresses.use_full_name)
51
+ when TrueClass, FalseClass
52
+ validates "#{category}_address", presence: (options == true)
53
+ validates "#{category}_address", effective_address_full_name_presence: EffectiveAddresses.use_full_name
54
+ else
55
+ raise 'Unexpected options. Expected a Hash or Boolean'
56
+ end
57
+ end
39
58
 
40
- else # Probably just true or false passed
41
- validates "#{category}_address", :presence => (options == true)
42
- validates "#{category}_address", :effective_address_full_name_presence => EffectiveAddresses.use_full_name
59
+ if ((categories.try(:keys) rescue nil) || categories).include?('billing') && ((categories.try(:keys) rescue nil) || categories).include?('shipping')
60
+ self.send(:define_method, :shipping_address_same_as_billing?) { billing_address == shipping_address }
43
61
 
44
- # Multiple Addresses
45
- self.send(:define_method, "#{category}_address=") { |atts| set_effective_address(category, atts) }
62
+ before_validation do
63
+ if @_effective_addresses_shipping_address_same_as_billing
64
+ self.shipping_address = self.billing_address
46
65
  end
47
66
  end
48
- else # No validation requirements passed
49
- categories.each do |category|
50
- category = category.to_s.gsub('_address', '')
51
67
 
52
- self.send(:define_method, "#{category}_address") { effective_address(category) }
53
- self.send(:define_method, "#{category}_addresses") { effective_addresses(category) }
54
- self.send(:define_method, "#{category}_address=") { |atts| set_effective_address(category, atts) }
55
-
56
- validates "#{category}_address", :effective_address_valid => true
57
- validates "#{category}_address", :effective_address_full_name_presence => EffectiveAddresses.use_full_name
58
- end
59
68
  end
69
+
60
70
  end
61
71
 
62
72
  module ClassMethods
@@ -86,9 +96,15 @@ module ActsAsAddressable
86
96
  :city => atts[:city],
87
97
  :state_code => atts[:state_code],
88
98
  :country_code => atts[:country_code],
89
- :postal_code => atts[:postal_code]
99
+ :postal_code => atts[:postal_code],
100
+ :shipping_address_same_as_billing => atts[:shipping_address_same_as_billing]
90
101
  )
91
102
 
103
+ if category == 'shipping' && add.shipping_address_same_as_billing?
104
+ @_effective_addresses_shipping_address_same_as_billing = true
105
+ return
106
+ end
107
+
92
108
  self.addresses << add unless (add == effective_address(category))
93
109
  end
94
110
 
@@ -97,7 +113,7 @@ module ActsAsAddressable
97
113
 
98
114
  atts = HashWithIndifferentAccess.new(atts.kind_of?(Effective::Address) ? atts.attributes : atts)
99
115
 
100
- (effective_address(category) || self.addresses.build()).tap do |existing|
116
+ add = (effective_address(category) || self.addresses.build()).tap do |existing|
101
117
  existing.category = category.to_s
102
118
  existing.full_name = atts[:full_name]
103
119
  existing.address1 = atts[:address1]
@@ -106,7 +122,13 @@ module ActsAsAddressable
106
122
  existing.state_code = atts[:state_code]
107
123
  existing.country_code = atts[:country_code]
108
124
  existing.postal_code = atts[:postal_code]
125
+ existing.shipping_address_same_as_billing = atts[:shipping_address_same_as_billing]
126
+ end
127
+
128
+ if category == 'shipping' && add.shipping_address_same_as_billing?
129
+ @_effective_addresses_shipping_address_same_as_billing = true
109
130
  end
131
+
110
132
  end
111
133
 
112
134
  end
@@ -1,11 +1,12 @@
1
1
  module Effective
2
2
  class Address < ActiveRecord::Base
3
3
  self.table_name = EffectiveAddresses.addresses_table_name.to_s
4
+ attr_accessor :shipping_address_same_as_billing
4
5
 
5
6
  POSTAL_CODE_CA = /\A[A-Z]{1}\d{1}[A-Z]{1}\ \d{1}[A-Z]{1}\d{1}\z/ # Matches 'T5Z 2B1'
6
7
  POSTAL_CODE_US = /\A\d{5}\z/ # Matches 5 digits
7
8
 
8
- belongs_to :addressable, :polymorphic => true, :touch => true
9
+ belongs_to :addressable, polymorphic: true, touch: true
9
10
 
10
11
  validates :category, :address1, :city, :country_code, :postal_code, presence: true, if: Proc.new { |address| address.present? }
11
12
  validates :state_code, presence: true, if: Proc.new { |address| address.present? && (address.country_code.blank? || Carmen::Country.coded(address.country_code).try(:subregions).present?) }
@@ -91,7 +92,7 @@ module Effective
91
92
  self_attrs = self.attributes
92
93
  other_attrs = other_address.respond_to?(:attributes) ? other_address.attributes : {}
93
94
 
94
- [self_attrs, other_attrs].each { |attrs| attrs.except!('id', 'created_at', 'updated_at', 'addressable_type', 'addressable_id') }
95
+ [self_attrs, other_attrs].each { |attrs| attrs.except!('id', 'created_at', 'updated_at', 'addressable_type', 'addressable_id', 'category') }
95
96
 
96
97
  self_attrs == other_attrs
97
98
  end
@@ -120,5 +121,10 @@ module Effective
120
121
  def to_html
121
122
  to_s.gsub(/\n/, "<br>\n").gsub(' ', '&nbsp;&nbsp;').html_safe
122
123
  end
124
+
125
+ def shipping_address_same_as_billing?
126
+ ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(self.shipping_address_same_as_billing)
127
+ end
128
+
123
129
  end
124
130
  end
@@ -1,32 +1,55 @@
1
1
  = f.simple_fields_for method, (address || f.object.send(method) || f.object.addresses.build(:category => method.to_s.gsub('_address', ''))), (EffectiveAddresses.simple_form_options || {}) do |fa|
2
2
  - uuid = fa.object_id
3
3
  - method = method.to_sym
4
+ - shipping_address_same_as_billing = (fa.object.category == 'shipping') && (f.object.try(:shipping_address_same_as_billing?) || false)
4
5
 
5
6
  - if ((f.object.errors.include?(method) && !f.object.errors.include?(:addresses)) rescue false)
6
7
  - fa.object.errors.add(:address1, f.object.errors[method].first)
7
8
 
9
+ - if fa.object.category == 'shipping' && f.object.respond_to?(:shipping_address_same_as_billing?)
10
+ = fa.input :shipping_address_same_as_billing,
11
+ as: :boolean,
12
+ label: 'My shipping address is the same as my billing address',
13
+ required: false,
14
+ input_html: { checked: shipping_address_same_as_billing }
15
+
8
16
  - field_order.each do |field|
9
17
 
10
18
  - case field
11
19
  - when :full_name
12
20
  - if use_full_name || fa.object.errors.include?(:full_name)
13
- = fa.input :full_name, required: required
21
+ = fa.input :full_name,
22
+ required: required,
23
+ disabled: shipping_address_same_as_billing,
24
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
14
25
 
15
26
  - when :address1
16
- = fa.input :address1, required: required
27
+ = fa.input :address1,
28
+ required: required,
29
+ disabled: shipping_address_same_as_billing,
30
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
17
31
 
18
32
  - when :address2
19
- = fa.input :address2
33
+ = fa.input :address2,
34
+ required: false,
35
+ disabled: shipping_address_same_as_billing,
36
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
20
37
 
21
38
  - when :city
22
- = fa.input :city, required: required, input_html: { 'data-effective-address-city' => uuid }
39
+ = fa.input :city,
40
+ required: required,
41
+ input_html: { 'data-effective-address-city' => uuid },
42
+ disabled: shipping_address_same_as_billing,
43
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
23
44
 
24
45
  - when :country_code
25
46
  = fa.input :country_code,
26
47
  as: (defined?(EffectiveFormInputs) ? :effective_select : :select),
27
48
  required: required,
28
49
  collection: region_options_for_simple_form_select(),
29
- input_html: {'data-effective-address-country' => uuid}
50
+ input_html: {'data-effective-address-country' => uuid},
51
+ disabled: shipping_address_same_as_billing,
52
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
30
53
 
31
54
  - when :state_code
32
55
  - if fa.object.try(:country_code).present?
@@ -36,16 +59,24 @@
36
59
  collection: region_options_for_simple_form_select(Carmen::Country.coded(fa.object.country_code).subregions),
37
60
  prompt: 'Please choose',
38
61
  input_html: {'data-effective-address-state' => uuid},
39
- input_js: { placeholder: 'Please choose' }
62
+ input_js: { placeholder: 'Please choose' },
63
+ disabled: shipping_address_same_as_billing,
64
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
40
65
  - else
41
66
  = fa.input :state_code,
42
67
  as: (defined?(EffectiveFormInputs) ? :effective_select : :select),
43
68
  required: required,
44
- disabled: true,
45
69
  collection: [],
46
70
  prompt: 'Please choose a country first',
47
71
  input_html: { 'data-effective-address-state' => uuid },
48
- input_js: { placeholder: 'Please choose a country first' }
72
+ input_js: { placeholder: 'Please choose a country first' },
73
+ disabled: true,
74
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
49
75
 
50
76
  - when :postal_code
51
- = fa.input :postal_code, required: required, input_html: { 'data-effective-address-postal-code' => uuid }
77
+ = fa.input :postal_code,
78
+ required: required,
79
+ input_html: { 'data-effective-address-postal-code' => uuid },
80
+ disabled: shipping_address_same_as_billing,
81
+ wrapper_html: { style: ('display: none;' if shipping_address_same_as_billing) }
82
+
@@ -1,3 +1,3 @@
1
1
  module EffectiveAddresses
2
- VERSION = '1.5.1'.freeze
2
+ VERSION = '1.6.0'.freeze
3
3
  end
@@ -21,7 +21,7 @@ module EffectiveAddresses
21
21
  end
22
22
 
23
23
  def self.permitted_params
24
- [:address1, :address2, :city, :country_code, :state_code, :postal_code, :full_name]
24
+ [:address1, :address2, :city, :country_code, :state_code, :postal_code, :full_name, :shipping_address_same_as_billing]
25
25
  end
26
26
 
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_addresses
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-14 00:00:00.000000000 Z
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -233,6 +233,7 @@ files:
233
233
  - Rakefile
234
234
  - app/assets/javascripts/effective_addresses.js
235
235
  - app/assets/javascripts/effective_addresses/address_fields.js.coffee
236
+ - app/assets/javascripts/effective_addresses/shipping_address_same_as_billing.js.coffee
236
237
  - app/controllers/effective/addresses_controller.rb
237
238
  - app/helpers/effective_addresses_helper.rb
238
239
  - app/models/concerns/acts_as_addressable.rb