effective_addresses 0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +1 -1
- data/README.md +103 -47
- data/app/assets/javascripts/effective_addresses/address_fields.js.coffee +7 -6
- data/app/controllers/effective/addresses_controller.rb +4 -4
- data/app/helpers/effective_addresses_helper.rb +37 -19
- data/app/models/concerns/acts_as_addressable.rb +58 -80
- data/app/models/effective/address.rb +64 -18
- data/app/models/validators/effective_address_full_name_presence_validator.rb +8 -0
- data/app/models/validators/effective_address_valid_validator.rb +7 -0
- data/app/views/effective/addresses/_address.html.haml +11 -0
- data/app/views/effective/addresses/_address_fields_formtastic.html.haml +32 -0
- data/app/views/effective/addresses/_address_fields_simple_form.html.haml +22 -0
- data/app/views/effective/addresses/_subregions.html.haml +1 -1
- data/config/routes.rb +7 -2
- data/lib/effective_addresses/engine.rb +1 -0
- data/lib/effective_addresses/version.rb +1 -1
- data/lib/effective_addresses.rb +8 -1
- data/lib/generators/templates/effective_addresses.rb +27 -5
- data/spec/controllers/addresses_controller_spec.rb +21 -1
- data/spec/dummy/log/test.log +0 -125
- data/spec/spec_helper.rb +1 -0
- metadata +41 -156
- data/app/models/validators/billing_address_validator.rb +0 -7
- data/app/views/effective/addresses/_address_fields.html.haml +0 -29
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +0 -103
@@ -0,0 +1,8 @@
|
|
1
|
+
class EffectiveAddressFullNamePresenceValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
if value.present? && value.full_name.blank?
|
4
|
+
record.errors[attribute] << "is invalid"
|
5
|
+
value.errors[:full_name] << "can't be blank"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
- if address.full_name.present?
|
2
|
+
= address.full_name
|
3
|
+
%br
|
4
|
+
= address.address1
|
5
|
+
%br
|
6
|
+
- if address.address2.present?
|
7
|
+
= address.address2
|
8
|
+
%br
|
9
|
+
= [city.presence, state.presence].compact.join(', ')
|
10
|
+
%br
|
11
|
+
= [country.presence, postal_code.presence].compact.join(', ')
|
@@ -0,0 +1,32 @@
|
|
1
|
+
= f.semantic_fields_for method, f.object.send(method) || f.object.addresses.build(:category => method.to_s.gsub('_address', '')) do |fa|
|
2
|
+
- uuid = fa.object_id
|
3
|
+
- method = method.to_sym
|
4
|
+
|
5
|
+
- if ((f.object.errors.include?(method) && !f.object.errors.include?(:addresses)) rescue false)
|
6
|
+
- fa.object.errors.add(:address1, f.object.errors[method].first)
|
7
|
+
|
8
|
+
- if f.object._validators[method].any? { |v| v.kind_of?(EffectiveAddressFullNamePresenceValidator) }
|
9
|
+
= fa.input :full_name, :required => true, :label => 'Full name', :prompt => 'Full name'
|
10
|
+
|
11
|
+
= fa.input :address1, :placeholder => 'Address', :label => "Address 1"
|
12
|
+
= fa.input :address2, :label => 'Address 2'
|
13
|
+
= fa.input :city, :placeholder => 'City'
|
14
|
+
= fa.input :country_code,
|
15
|
+
:as => :select,
|
16
|
+
:label => 'Country',
|
17
|
+
:prompt => 'Country...',
|
18
|
+
:collection => region_options_for_select(EffectiveAddresses.country_codes == :all ? Carmen::Country.all : Carmen::Country.all.select{ |c| EffectiveAddresses.country_codes.include?(c.code) rescue true}, fa.object.country_code, :priority => EffectiveAddresses.country_codes_priority),
|
19
|
+
:input_html => { 'data-effective-address-country' => uuid }
|
20
|
+
|
21
|
+
- if fa.object.country_code.present?
|
22
|
+
= fa.input :state_code, :as => :select, :label => 'Province / State',
|
23
|
+
:collection => region_options_for_select(Carmen::Country.coded(fa.object.country_code).subregions, fa.object.state_code),
|
24
|
+
:prompt => 'please select a country',
|
25
|
+
:input_html => { 'data-effective-address-state' => uuid }
|
26
|
+
- else
|
27
|
+
= fa.input :state_code, :as => :select, :label => 'Province / State',
|
28
|
+
:collection => [],
|
29
|
+
:prompt => 'please select a country',
|
30
|
+
:input_html => {:disabled => 'disabled', 'data-effective-address-state' => uuid}
|
31
|
+
|
32
|
+
= fa.input :postal_code, :label => 'Postal / Zip code', :placeholder => 'Postal / Zip code'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
= f.simple_fields_for method, (f.object.send(method) || f.object.addresses.build(:category => method.to_s.gsub('_address', ''))), (EffectiveAddresses.simple_form_options || {}) do |fa|
|
2
|
+
- uuid = fa.object_id
|
3
|
+
- method = method.to_sym
|
4
|
+
|
5
|
+
- if ((f.object.errors.include?(method) && !f.object.errors.include?(:addresses)) rescue false)
|
6
|
+
- fa.object.errors.add(:address1, f.object.errors[method].first)
|
7
|
+
|
8
|
+
- if f.object._validators[method].any? { |v| v.kind_of?(EffectiveAddressFullNamePresenceValidator) }
|
9
|
+
= fa.input :full_name, :required => true, :label => 'Full name', :prompt => 'Full name'
|
10
|
+
|
11
|
+
= fa.input :address1, :placeholder => 'Address', :label => 'Address 1', :required => required
|
12
|
+
= fa.input :address2, :label => 'Address 2'
|
13
|
+
= fa.input :city, :placeholder => 'City', :required => required
|
14
|
+
|
15
|
+
= fa.input :country_code, :as => :select, :label => 'Country', :prompt => 'Choose country...', :collection => region_options_for_simple_form_select(), :input_html => {'data-effective-address-country' => uuid}, :required => required
|
16
|
+
|
17
|
+
- if fa.object.try(:country_code).present?
|
18
|
+
= fa.input :state_code, :as => :select, :label => 'Province / State', :prompt => 'Please choose a country first', :collection => region_options_for_simple_form_select(Carmen::Country.coded(fa.object.country_code).subregions), :input_html => {'data-effective-address-state' => uuid}, :required => required
|
19
|
+
- else
|
20
|
+
= fa.input :state_code, :as => :select, :label => 'Province / State', :disabled => true, :prompt => 'Please choose a country', :collection => [], :input_html => { 'data-effective-address-state' => uuid }, :required => required
|
21
|
+
|
22
|
+
= fa.input :postal_code, :label => 'Postal / Zip code', :placeholder => 'Postal / Zip code', :required => required
|
@@ -1 +1 @@
|
|
1
|
-
= region_options_for_select(
|
1
|
+
= region_options_for_select(@subregions)
|
data/config/routes.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
# These will be just added to the list of routes in the main application
|
2
1
|
Rails.application.routes.draw do
|
3
|
-
|
2
|
+
mount EffectiveAddresses::Engine => '/', :as => 'effective_addresses'
|
3
|
+
end
|
4
|
+
|
5
|
+
EffectiveAddresses::Engine.routes.draw do
|
6
|
+
scope :module => 'effective' do
|
7
|
+
match 'addresses/subregions/:country_code', :to => 'addresses#subregions', :as => 'address_subregions', :via => :get
|
8
|
+
end
|
4
9
|
end
|
data/lib/effective_addresses.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "effective_addresses/engine"
|
2
2
|
require "effective_addresses/version"
|
3
|
-
require 'migrant' # Required for rspec to run properly
|
3
|
+
#require 'migrant' # Required for rspec to run properly
|
4
4
|
|
5
5
|
module EffectiveAddresses
|
6
6
|
|
@@ -8,9 +8,16 @@ module EffectiveAddresses
|
|
8
8
|
mattr_accessor :country_codes
|
9
9
|
mattr_accessor :country_codes_priority
|
10
10
|
mattr_accessor :addresses_table_name
|
11
|
+
mattr_accessor :use_full_name
|
12
|
+
mattr_accessor :simple_form_options
|
13
|
+
mattr_accessor :validate_postal_code_format
|
11
14
|
|
12
15
|
def self.setup
|
13
16
|
yield self
|
14
17
|
end
|
15
18
|
|
19
|
+
def self.permitted_params
|
20
|
+
[:address1, :address2, :city, :country_code, :state_code, :postal_code, :full_name]
|
21
|
+
end
|
22
|
+
|
16
23
|
end
|
@@ -2,11 +2,33 @@ EffectiveAddresses.setup do |config|
|
|
2
2
|
# Database table name to store addresses in. Default is :addresses
|
3
3
|
config.addresses_table_name = :addresses
|
4
4
|
|
5
|
+
# Display Full Name on Address forms, and validate presence by default
|
6
|
+
# (can be overridden on a per address basis)
|
7
|
+
config.use_full_name = true
|
8
|
+
|
5
9
|
# Country codes to display in country_select dropdowns.
|
6
|
-
config.country_codes = :all
|
7
|
-
config.
|
10
|
+
config.country_codes = :all #
|
11
|
+
#config.country_codes = ['US', 'CA'] # Or you can be more selective...
|
12
|
+
|
13
|
+
# Select these countries ontop of the others
|
14
|
+
config.country_codes_priority = ['US', 'CA'] # Leave empty array for no priority countries
|
15
|
+
|
16
|
+
# Validate that the postal/zip code format is correct for these countries
|
17
|
+
# Right now, only US and Canada are supported
|
18
|
+
config.validate_postal_code_format = ['US', 'CA']
|
19
|
+
|
20
|
+
# SimpleForm Options
|
21
|
+
# This Hash of options will be passed into any simple_form_for() calls
|
22
|
+
config.simple_form_options = {}
|
23
|
+
|
24
|
+
# config.simple_form_options = {
|
25
|
+
# :html => {:class => 'form-horizontal'},
|
26
|
+
# :wrapper => :horizontal_form,
|
27
|
+
# :wrapper_mappings => {
|
28
|
+
# :boolean => :horizontal_boolean,
|
29
|
+
# :check_boxes => :horizontal_radio_and_checkboxes,
|
30
|
+
# :radio_buttons => :horizontal_radio_and_checkboxes
|
31
|
+
# }
|
32
|
+
# }
|
8
33
|
|
9
|
-
# Or you can be more selective...
|
10
|
-
#config.country_codes = ['US', 'CA']
|
11
|
-
#config.country_codes_priority = ['US', 'CA']
|
12
34
|
end
|
@@ -1,10 +1,30 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Effective::AddressesController do
|
4
|
-
it '
|
4
|
+
it 'should render the subregions partial' do
|
5
5
|
get :subregions, :country_code => 'CA'
|
6
6
|
response.status.should be 200
|
7
|
+
response.should render_template 'effective/addresses/_subregions'
|
7
8
|
end
|
8
9
|
|
10
|
+
it 'should assign appropriate Canadian subregions' do
|
11
|
+
get :subregions, :country_code => 'CA'
|
12
|
+
assigns(:subregions).first.name.should eq 'Alberta'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should assign appropriate US subregions' do
|
16
|
+
get :subregions, :country_code => 'US'
|
17
|
+
assigns(:subregions).first.name.should eq 'Alaska'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should assign appropriate SG subregions' do
|
21
|
+
get :subregions, :country_code => 'SG' # Singapore
|
22
|
+
assigns(:subregions).first.name.should eq 'Central Singapore'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'Should return an error when passed bad country code' do
|
26
|
+
get :subregions, :country_code => 'NOPE'
|
27
|
+
response.status.should be 422 # Unprocessable Entity
|
28
|
+
end
|
9
29
|
|
10
30
|
end
|
data/spec/dummy/log/test.log
CHANGED
@@ -1,125 +0,0 @@
|
|
1
|
-
Connecting to database specified by database.yml
|
2
|
-
Connecting to database specified by database.yml
|
3
|
-
Connecting to database specified by database.yml
|
4
|
-
Connecting to database specified by database.yml
|
5
|
-
Connecting to database specified by database.yml
|
6
|
-
Connecting to database specified by database.yml
|
7
|
-
Connecting to database specified by database.yml
|
8
|
-
Connecting to database specified by database.yml
|
9
|
-
Connecting to database specified by database.yml
|
10
|
-
Connecting to database specified by database.yml
|
11
|
-
Connecting to database specified by database.yml
|
12
|
-
Connecting to database specified by database.yml
|
13
|
-
Connecting to database specified by database.yml
|
14
|
-
Connecting to database specified by database.yml
|
15
|
-
Connecting to database specified by database.yml
|
16
|
-
Connecting to database specified by database.yml
|
17
|
-
Connecting to database specified by database.yml
|
18
|
-
Connecting to database specified by database.yml
|
19
|
-
Connecting to database specified by database.yml
|
20
|
-
Connecting to database specified by database.yml
|
21
|
-
Connecting to database specified by database.yml
|
22
|
-
Connecting to database specified by database.yml
|
23
|
-
Connecting to database specified by database.yml
|
24
|
-
Connecting to database specified by database.yml
|
25
|
-
Connecting to database specified by database.yml
|
26
|
-
Connecting to database specified by database.yml
|
27
|
-
Connecting to database specified by database.yml
|
28
|
-
Connecting to database specified by database.yml
|
29
|
-
Connecting to database specified by database.yml
|
30
|
-
Connecting to database specified by database.yml
|
31
|
-
Connecting to database specified by database.yml
|
32
|
-
Connecting to database specified by database.yml
|
33
|
-
Connecting to database specified by database.yml
|
34
|
-
Connecting to database specified by database.yml
|
35
|
-
Connecting to database specified by database.yml
|
36
|
-
Connecting to database specified by database.yml
|
37
|
-
Connecting to database specified by database.yml
|
38
|
-
Connecting to database specified by database.yml
|
39
|
-
Connecting to database specified by database.yml
|
40
|
-
Connecting to database specified by database.yml
|
41
|
-
Connecting to database specified by database.yml
|
42
|
-
Connecting to database specified by database.yml
|
43
|
-
Connecting to database specified by database.yml
|
44
|
-
Connecting to database specified by database.yml
|
45
|
-
Connecting to database specified by database.yml
|
46
|
-
Connecting to database specified by database.yml
|
47
|
-
Connecting to database specified by database.yml
|
48
|
-
Connecting to database specified by database.yml
|
49
|
-
Connecting to database specified by database.yml
|
50
|
-
Connecting to database specified by database.yml
|
51
|
-
Connecting to database specified by database.yml
|
52
|
-
Connecting to database specified by database.yml
|
53
|
-
Connecting to database specified by database.yml
|
54
|
-
Connecting to database specified by database.yml
|
55
|
-
Connecting to database specified by database.yml
|
56
|
-
Connecting to database specified by database.yml
|
57
|
-
Connecting to database specified by database.yml
|
58
|
-
Connecting to database specified by database.yml
|
59
|
-
Connecting to database specified by database.yml
|
60
|
-
Connecting to database specified by database.yml
|
61
|
-
Connecting to database specified by database.yml
|
62
|
-
Connecting to database specified by database.yml
|
63
|
-
Connecting to database specified by database.yml
|
64
|
-
Connecting to database specified by database.yml
|
65
|
-
Connecting to database specified by database.yml
|
66
|
-
Connecting to database specified by database.yml
|
67
|
-
Connecting to database specified by database.yml
|
68
|
-
Connecting to database specified by database.yml
|
69
|
-
Connecting to database specified by database.yml
|
70
|
-
Connecting to database specified by database.yml
|
71
|
-
Connecting to database specified by database.yml
|
72
|
-
Connecting to database specified by database.yml
|
73
|
-
Connecting to database specified by database.yml
|
74
|
-
Connecting to database specified by database.yml
|
75
|
-
Connecting to database specified by database.yml
|
76
|
-
Connecting to database specified by database.yml
|
77
|
-
Connecting to database specified by database.yml
|
78
|
-
Connecting to database specified by database.yml
|
79
|
-
Connecting to database specified by database.yml
|
80
|
-
Connecting to database specified by database.yml
|
81
|
-
Connecting to database specified by database.yml
|
82
|
-
Connecting to database specified by database.yml
|
83
|
-
Connecting to database specified by database.yml
|
84
|
-
Connecting to database specified by database.yml
|
85
|
-
Connecting to database specified by database.yml
|
86
|
-
Connecting to database specified by database.yml
|
87
|
-
Connecting to database specified by database.yml
|
88
|
-
Connecting to database specified by database.yml
|
89
|
-
Connecting to database specified by database.yml
|
90
|
-
Connecting to database specified by database.yml
|
91
|
-
Connecting to database specified by database.yml
|
92
|
-
Connecting to database specified by database.yml
|
93
|
-
Connecting to database specified by database.yml
|
94
|
-
Connecting to database specified by database.yml
|
95
|
-
Connecting to database specified by database.yml
|
96
|
-
Connecting to database specified by database.yml
|
97
|
-
Connecting to database specified by database.yml
|
98
|
-
Connecting to database specified by database.yml
|
99
|
-
Connecting to database specified by database.yml
|
100
|
-
Connecting to database specified by database.yml
|
101
|
-
Connecting to database specified by database.yml
|
102
|
-
Connecting to database specified by database.yml
|
103
|
-
Connecting to database specified by database.yml
|
104
|
-
Connecting to database specified by database.yml
|
105
|
-
Connecting to database specified by database.yml
|
106
|
-
Connecting to database specified by database.yml
|
107
|
-
Connecting to database specified by database.yml
|
108
|
-
Connecting to database specified by database.yml
|
109
|
-
Connecting to database specified by database.yml
|
110
|
-
Connecting to database specified by database.yml
|
111
|
-
Connecting to database specified by database.yml
|
112
|
-
Connecting to database specified by database.yml
|
113
|
-
Connecting to database specified by database.yml
|
114
|
-
Connecting to database specified by database.yml
|
115
|
-
Connecting to database specified by database.yml
|
116
|
-
Connecting to database specified by database.yml
|
117
|
-
Connecting to database specified by database.yml
|
118
|
-
Connecting to database specified by database.yml
|
119
|
-
Connecting to database specified by database.yml
|
120
|
-
Connecting to database specified by database.yml
|
121
|
-
Connecting to database specified by database.yml
|
122
|
-
Connecting to database specified by database.yml
|
123
|
-
Connecting to database specified by database.yml
|
124
|
-
Connecting to database specified by database.yml
|
125
|
-
Connecting to database specified by database.yml
|
data/spec/spec_helper.rb
CHANGED
@@ -14,6 +14,7 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f }
|
|
14
14
|
Carmen.i18n_backend.load_path << (Carmen.root_path.to_s + '/locale/en/world.yml')
|
15
15
|
Carmen.i18n_backend.load_path << (Carmen.root_path.to_s + '/locale/en/world/ca.yml')
|
16
16
|
Carmen.i18n_backend.load_path << (Carmen.root_path.to_s + '/locale/en/world/us.yml')
|
17
|
+
Carmen.i18n_backend.load_path << (Carmen.root_path.to_s + '/locale/en/world/sg.yml')
|
17
18
|
|
18
19
|
RSpec.configure do |config|
|
19
20
|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|