address_concern 2.0.0 → 2.1.1
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.
- checksums.yaml +4 -4
- data/Readme.md +73 -8
- data/address_concern.gemspec +3 -4
- data/config/address_concern.rb +3 -0
- data/lib/address_concern/address.rb +582 -104
- data/lib/address_concern/address_associations.rb +33 -12
- data/lib/address_concern/attribute_normalizer.rb +10 -6
- data/lib/address_concern/attributes_slice.rb +54 -0
- data/lib/address_concern/engine.rb +18 -0
- data/lib/address_concern/inspect_base.rb +33 -0
- data/lib/address_concern/version.rb +1 -1
- data/lib/address_concern.rb +17 -4
- 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 +16 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd8e4df80024c5fc3a3bc5984003e412615c3d6345d8408d17859e088183e7f2
|
4
|
+
data.tar.gz: eda7df98ac44d1ecc07c7e5a0f3c4afe3c93baa53378b31cdeab6d93a19d6d34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a09d2db18101b7b407e52481559cefd69ae17b06384e448e92b160c939d785045725e3c768156a264af182087f1a11a77f1ddc0f5b9e3a9f3202b6cfdce8d719
|
7
|
+
data.tar.gz: 7ba89fd64d9f910fe77e5b807f335c6923ca80289549a6c43afe435ed4cbad699196c75f2415f8479b474e63f86943764e881b475e1e7cb2a76fcbbd5f88ec40
|
data/Readme.md
CHANGED
@@ -6,25 +6,55 @@ 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
|
+
```
|
12
|
+
|
13
|
+
Include the `AddressConcern::Address` concern in your app's `Address` model by adding the
|
14
|
+
`acts_as_address` macro to it:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
class Address < ApplicationRecord
|
18
|
+
acts_as_address
|
19
|
+
end
|
20
|
+
```
|
10
21
|
|
11
22
|
Then run the generator to create your addresses table:
|
12
23
|
|
13
24
|
rails generate address_concern:install
|
14
|
-
|
25
|
+
rails db:migrate
|
15
26
|
|
16
|
-
You
|
27
|
+
You can modify the migration and add any other fields you may wish to include.
|
17
28
|
|
18
|
-
|
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.
|
19
35
|
|
20
|
-
|
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:
|
21
42
|
|
22
43
|
```ruby
|
23
44
|
class Address < ApplicationRecord
|
24
|
-
|
45
|
+
acts_as_address(
|
46
|
+
country: {
|
47
|
+
code_attribute: :country,
|
48
|
+
},
|
49
|
+
state: {
|
50
|
+
code_attribute: :state,
|
51
|
+
},
|
52
|
+
)
|
25
53
|
end
|
26
54
|
```
|
27
55
|
|
56
|
+
# Usage
|
57
|
+
|
28
58
|
## `belongs_to_address`
|
29
59
|
|
30
60
|
`AddressConcern::AddressAssociations` is automatically included into `ActiveRecord::Base` and
|
@@ -113,10 +143,19 @@ address = company.build_address(address: '...')
|
|
113
143
|
```
|
114
144
|
|
115
145
|
This also adds a polymorphic `addressable` association on the Address model (not available if you're
|
116
|
-
using `
|
146
|
+
using `belongs_to_address` on your addressable models instead of `has_address`):
|
117
147
|
|
118
148
|
```ruby
|
119
|
-
belongs_to :addressable, :
|
149
|
+
belongs_to :addressable, polymorphic: true, touch: true, optional: true
|
150
|
+
```
|
151
|
+
|
152
|
+
If you wish to customize that `belongs_to`, you can pass in any options you like:
|
153
|
+
```ruby
|
154
|
+
class Address < ApplicationRecord
|
155
|
+
include AddressConcern::Address
|
156
|
+
|
157
|
+
belongs_to_addressable options…
|
158
|
+
end
|
120
159
|
```
|
121
160
|
|
122
161
|
## `has_addresses`
|
@@ -154,6 +193,32 @@ vacation_address = user.addresses.build(address: 'Vacation', :address_type => 'V
|
|
154
193
|
user.addresses # => [shipping_address, vacation_address]
|
155
194
|
```
|
156
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
|
+
|
157
222
|
# Country/state database
|
158
223
|
|
159
224
|
Country/state data comes from the [`carmen`](https://github.com/carmen-ruby/carmen) gem.
|
data/address_concern.gemspec
CHANGED
@@ -13,15 +13,14 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.licenses = ["MIT"]
|
14
14
|
|
15
15
|
s.add_dependency "rake"
|
16
|
-
s.add_dependency "cucumber"
|
17
16
|
s.add_dependency "rails", ">= 4.0"
|
18
17
|
s.add_dependency "activerecord", ">= 4.0"
|
19
18
|
s.add_dependency "activesupport", ">= 4.0"
|
20
19
|
s.add_dependency "carmen", '>= 1.1.1'
|
21
|
-
s.add_dependency "attribute_normalizer"
|
22
|
-
s.add_dependency "
|
23
|
-
s.add_dependency "facets"
|
20
|
+
#s.add_dependency "attribute_normalizer"
|
21
|
+
s.add_dependency "zeitwerk"
|
24
22
|
|
23
|
+
s.add_development_dependency 'active_record_ignored_attributes' # for be_same_as
|
25
24
|
s.add_development_dependency 'rspec'
|
26
25
|
s.add_development_dependency 'sqlite3'
|
27
26
|
#s.add_development_dependency 'mysql2', '~>0.2.11'
|