address_concern 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Readme.md +73 -6
- data/address_concern.gemspec +2 -3
- data/app/models/concerns/address.rb +784 -0
- data/{lib/address_concern → app/models/concerns}/address_associations.rb +33 -12
- data/app/models/concerns/attributes_slice.rb +54 -0
- data/app/models/concerns/inspect_base.rb +32 -0
- data/config/address_concern.rb +3 -0
- data/lib/address_concern/attribute_normalizer.rb +10 -6
- data/lib/address_concern/engine.rb +24 -0
- data/lib/address_concern/version.rb +1 -1
- data/lib/address_concern.rb +4 -3
- data/lib/core_extensions/hash/reorder.rb +11 -0
- data/lib/core_extensions/string/cleanlines.rb +28 -0
- data/lib/generators/address_concern/templates/migration.rb +14 -10
- data/spec/models/acts_as_address_spec.rb +66 -0
- data/spec/models/address_spec.rb +345 -117
- data/spec/spec_helper.rb +1 -5
- data/spec/support/models/address.rb +2 -1
- data/spec/support/models/address_custom_attr_names.rb +11 -0
- data/spec/support/models/address_with_code_only.rb +12 -0
- data/spec/support/models/address_with_name_only.rb +5 -0
- data/spec/support/models/address_with_separate_address_columns.rb +5 -0
- data/spec/support/models/user.rb +6 -1
- data/spec/support/schema.rb +22 -0
- metadata +17 -34
- data/lib/address_concern/address.rb +0 -306
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 205541c7ef9aedeb7e807bb946274c5a379edc7162592bbb2b1fd1015ce681b5
|
4
|
+
data.tar.gz: 2dacfdb3da70d8ef88989ea2966e407179af9d536b8e3ae5df0826abe7ab1571
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15f9277026adf1a62dff7561386dae79d021f11eeb3f3b4c3c13ff76bb991c7893861402ad120ec7ff8a4e714fd389cb644580a1a7f2a2fedc4c273018b77c38
|
7
|
+
data.tar.gz: a5ab51814edaee41313de51bf2fc7fa75b683b79d490dc26af1401b3770c292c4ffd49b0452b3a7f02b1fd49b1d48fa2e052087d8b8e9ae43126dfa6b6fc5fe6
|
data/Readme.md
CHANGED
@@ -6,20 +6,52 @@ A reusable polymorphic `Address` model concern for your Rails apps.
|
|
6
6
|
|
7
7
|
Add `address_concern` to your `Gemfile`:
|
8
8
|
|
9
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'address_concern'
|
11
|
+
```
|
10
12
|
|
11
|
-
Include the `AddressConcern::Address` concern in your model
|
13
|
+
Include the `AddressConcern::Address` concern in your app's `Address` model by adding the
|
14
|
+
`acts_as_address` macro to it:
|
12
15
|
|
13
16
|
```ruby
|
14
17
|
class Address < ApplicationRecord
|
15
|
-
|
18
|
+
acts_as_address
|
16
19
|
end
|
17
20
|
```
|
18
21
|
|
19
22
|
Then run the generator to create your addresses table:
|
20
23
|
|
21
24
|
rails generate address_concern:install
|
22
|
-
|
25
|
+
rails db:migrate
|
26
|
+
|
27
|
+
You can modify the migration and add any other fields you may wish to include.
|
28
|
+
|
29
|
+
For country and state/providence, you may choose to store both the code and name or just code or
|
30
|
+
just name. Remove from the migration the columns you don't need.
|
31
|
+
|
32
|
+
By default, it will store country name in `country_name` or `country` if one of those columns exist,
|
33
|
+
and store country code in `country_code` or `country` if one of those columns exist. If _only_ a
|
34
|
+
`country` column exists, it will be used to store the name attribute by default.
|
35
|
+
|
36
|
+
By default, it will store state name in `state_name` or `state` if one of those columns exist,
|
37
|
+
and store state code in `state_code` or `state` if one of those columns exist. If _only_ a
|
38
|
+
`state` column exists, it will be used to store the name attribute by default.
|
39
|
+
|
40
|
+
These column names can be configured. For example, to store country code in `country` and state code
|
41
|
+
in `state`, you could do:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class Address < ApplicationRecord
|
45
|
+
acts_as_address(
|
46
|
+
country: {
|
47
|
+
code_attribute: :country,
|
48
|
+
},
|
49
|
+
state: {
|
50
|
+
code_attribute: :state,
|
51
|
+
},
|
52
|
+
)
|
53
|
+
end
|
54
|
+
```
|
23
55
|
|
24
56
|
# Usage
|
25
57
|
|
@@ -111,10 +143,19 @@ address = company.build_address(address: '...')
|
|
111
143
|
```
|
112
144
|
|
113
145
|
This also adds a polymorphic `addressable` association on the Address model (not available if you're
|
114
|
-
using `
|
146
|
+
using `belongs_to_address` on your addressable models instead of `has_address`):
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
belongs_to :addressable, polymorphic: true, touch: true, optional: true
|
150
|
+
```
|
115
151
|
|
152
|
+
If you wish to customize that `belongs_to`, you can pass in any options you like:
|
116
153
|
```ruby
|
117
|
-
|
154
|
+
class Address < ApplicationRecord
|
155
|
+
include AddressConcern::Address
|
156
|
+
|
157
|
+
belongs_to_addressable options…
|
158
|
+
end
|
118
159
|
```
|
119
160
|
|
120
161
|
## `has_addresses`
|
@@ -152,6 +193,32 @@ vacation_address = user.addresses.build(address: 'Vacation', :address_type => 'V
|
|
152
193
|
user.addresses # => [shipping_address, vacation_address]
|
153
194
|
```
|
154
195
|
|
196
|
+
## Street address
|
197
|
+
|
198
|
+
You are free to either store the street address in a single column like this:
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
create_table :addresses do |t|
|
202
|
+
…
|
203
|
+
t.text :address
|
204
|
+
…
|
205
|
+
```
|
206
|
+
|
207
|
+
or in separate columns like this:
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
create_table :addresses do |t|
|
211
|
+
…
|
212
|
+
t.string :address_1
|
213
|
+
t.string :address_2
|
214
|
+
t.string :address_3
|
215
|
+
…
|
216
|
+
```
|
217
|
+
|
218
|
+
If you store it in a single column of type text, then it will support multi-line addresses stored in
|
219
|
+
that single column. Calling `address.address_lines`, for example, will return an array of address
|
220
|
+
lines — however many lines the user entered (you may add validations to limit this as you wish).
|
221
|
+
|
155
222
|
# Country/state database
|
156
223
|
|
157
224
|
Country/state data comes from the [`carmen`](https://github.com/carmen-ruby/carmen) gem.
|
data/address_concern.gemspec
CHANGED
@@ -17,10 +17,9 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.add_dependency "activerecord", ">= 4.0"
|
18
18
|
s.add_dependency "activesupport", ">= 4.0"
|
19
19
|
s.add_dependency "carmen", '>= 1.1.1'
|
20
|
-
s.add_dependency "attribute_normalizer"
|
21
|
-
s.add_dependency "active_record_ignored_attributes"
|
22
|
-
s.add_dependency "facets"
|
20
|
+
#s.add_dependency "attribute_normalizer"
|
23
21
|
|
22
|
+
s.add_development_dependency 'active_record_ignored_attributes' # for be_same_as
|
24
23
|
s.add_development_dependency 'rspec'
|
25
24
|
s.add_development_dependency 'sqlite3'
|
26
25
|
#s.add_development_dependency 'mysql2', '~>0.2.11'
|