magic_addresses 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +37 -2
  3. data/app/models/magic_addresses/address.rb +3 -11
  4. data/app/models/magic_addresses/association.rb +35 -15
  5. data/app/models/magic_addresses/country.rb +5 -0
  6. data/app/models/magic_addresses/district.rb +4 -0
  7. data/app/models/magic_addresses/state.rb +4 -0
  8. data/app/models/magic_addresses/subdistrict.rb +2 -0
  9. data/lib/generators/magic_addresses/templates/magic_addresses_migration.rb +1 -10
  10. data/lib/generators/magic_addresses/templates/magic_initializer.rb +0 -2
  11. data/lib/helpers/mgca_helper.rb +6 -13
  12. data/lib/magic_addresses/configuration.rb +0 -2
  13. data/lib/magic_addresses/version.rb +1 -1
  14. data/spec/dummy/config/initializers/magic_addresses.rb +0 -2
  15. data/spec/dummy/db/development.sqlite3 +0 -0
  16. data/spec/dummy/db/migrate/{20150223223200_add_magic_addresses.rb → 20150225220219_add_magic_addresses.rb} +1 -10
  17. data/spec/dummy/db/schema.rb +1 -1
  18. data/spec/dummy/db/test.sqlite3 +0 -0
  19. data/spec/dummy/log/development.log +3729 -0
  20. data/spec/dummy/log/test.log +37882 -0
  21. data/spec/models/magic_addresses/address_spec.rb +171 -0
  22. data/spec/models/magic_addresses/association_spec.rb +93 -8
  23. metadata +4 -28
  24. data/app/models/concerns/address_builder.rb +0 -169
  25. data/app/models/concerns/has_address.rb +0 -67
  26. data/app/models/location/address.rb +0 -111
  27. data/app/models/location/city.rb +0 -39
  28. data/app/models/location/country.rb +0 -39
  29. data/app/models/location/district.rb +0 -37
  30. data/app/models/location/state.rb +0 -39
  31. data/app/models/location/subdistrict.rb +0 -38
  32. data/app/services/geo_coder.rb +0 -46
  33. data/lib/generators/magic_addresses/templates/location_addresses_migration.rb +0 -157
  34. data/spec/models/concerns/has_address_spec.rb +0 -92
  35. data/spec/models/location/address_spec.rb +0 -227
  36. data/spec/models/location/city_spec.rb +0 -27
  37. data/spec/models/location/country_spec.rb +0 -26
  38. data/spec/models/location/state_spec.rb +0 -27
  39. data/spec/models/location/subcity_spec.rb +0 -26
  40. data/spec/models/location/subsubcity_spec.rb +0 -27
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: beb2e21a6fbf9f4f598bd5d134e5600904a727fe
4
- data.tar.gz: c80f9082f1680764ba4f7261f833ea5a89c24736
3
+ metadata.gz: afae17ac192fd28b6715937a4a9d213298b208a1
4
+ data.tar.gz: 884a80a98cc530e7d53f128d9c0d96b22d269536
5
5
  SHA512:
6
- metadata.gz: 1a7776f89b502f208b03171daa36c34e5287f5ae37868538ee2dc769fe776023f9a214c064105ff6e8399cf8ebe4ec2097b7e1cecfc332f70f044b8d6569dae8
7
- data.tar.gz: f6b7297fb37c484df43177548c36689b235b95b4aaf292903ca34f6365ee6f15022b0f01ebd78d4bfa4b05baebce694a17e2422a78ef0b818b19ec572a80ce64
6
+ metadata.gz: 5c6854f10dedc764d2a539bbdff5dbb03c5cc56c95a3f0b0e0db40984926653b5f514a9982743ae86775560cf433b681b408603197ee6d1943012a49c68407f9
7
+ data.tar.gz: 943f8b826fbedb192e86d60a34195142fa17a966f39b6fd6ea4d25157eb381aedf8781d7631bc2aa7466289208a6f9bc5091fd134ecbe84e1398e73441821def
data/README.md CHANGED
@@ -25,12 +25,47 @@ rails g magic_addresses:install
25
25
  # You can use `has_one_address` and `has_addresses` on the same model
26
26
  # `has_one_address` sets the default flag so could be major address.
27
27
 
28
-
29
- # in Progress
30
28
  has_nested_address # => Has one directly nested addresses. (ie: User.street, User.city)
31
29
 
32
30
  ```
33
31
 
32
+ #### View Mehtods
33
+
34
+ ```ruby
35
+
36
+ <%= country_flag( :de, "small" ) %>
37
+ # flag helper for all default countries ( "small" | "medium" | "large")
38
+
39
+ <%= render "magic_addresses/addresses/fields/address", f: f %>
40
+ # form helper for 'has_one_address'
41
+
42
+ ```
43
+
44
+ #### in your Controllers:
45
+
46
+ ```ruby
47
+
48
+ private
49
+
50
+ # Never trust parameters from the scary internet! ... has_one_address
51
+ def instance_params
52
+ params.require(:instance).permit( .. :address_attributes => [:id, :street, :number, :postalcode, :city, :country, :_destroy] .. )
53
+ end
54
+
55
+
56
+ # Never trust parameters from the scary internet! ... has_addresses
57
+ def instance_params
58
+ params.require(:instance).permit( .. :addresses_attributes => [:id, :street, :number, :postalcode, :city, :country, :_destroy] .. )
59
+ end
60
+
61
+ - or -
62
+
63
+ :address_attributes => MagicAddresses::Address::PARAMS # has_one_address
64
+ :addresses_attributes => MagicAddresses::Address::PARAMS # has_addresses
65
+
66
+ ```
67
+
68
+
34
69
 
35
70
  #### Structure
36
71
 
@@ -6,6 +6,8 @@ class MagicAddresses::Address < ActiveRecord::Base
6
6
 
7
7
 
8
8
  # =====> C O N S T A N T S <=============================================================== #
9
+ PARAMS = [ :id, :street, :street_additional, :number, :postalcode, :city, :country, :owner, :_destroy ]
10
+
9
11
 
10
12
  # =====> A S S O Z I A T I O N S <========================================================= #
11
13
  belongs_to :owner, polymorphic: true
@@ -28,15 +30,9 @@ class MagicAddresses::Address < ActiveRecord::Base
28
30
 
29
31
 
30
32
  # =====> A T T R I B U T E S <============================================================= #
31
- if MagicAddresses.configuration.hstore
32
- # setup hstore
33
- store_accessor :fetch_address # , :fetch_street, :fetch_number, :fetch_city, :fetch_zipcode, :fetch_country
34
- else
35
- serialize :fetch_address, Hash
36
- end
33
+ serialize :fetch_address, Hash
37
34
 
38
35
  %w[fetch_street fetch_number fetch_city fetch_zipcode fetch_country].each do |key|
39
- # attr_accessor key
40
36
  define_method(key) do
41
37
  fetch_address && fetch_address[key]
42
38
  end
@@ -44,7 +40,6 @@ class MagicAddresses::Address < ActiveRecord::Base
44
40
  self.fetch_address = (fetch_address || {}).merge(key => value)
45
41
  end
46
42
  end
47
-
48
43
  # attr_accessor :street
49
44
  def street
50
45
  fetch_address && fetch_address["fetch_street"] || street_name
@@ -53,7 +48,6 @@ class MagicAddresses::Address < ActiveRecord::Base
53
48
  self.street_name = value
54
49
  self.fetch_address = (fetch_address || {}).merge("fetch_street" => value)
55
50
  end
56
-
57
51
  # attr_accessor :number (:street_number)
58
52
  def number
59
53
  fetch_address && fetch_address["fetch_number"] || street_number
@@ -61,7 +55,6 @@ class MagicAddresses::Address < ActiveRecord::Base
61
55
  def number=(value)
62
56
  self.fetch_address = (fetch_address || {}).merge("fetch_number" => value)
63
57
  end
64
-
65
58
  # attr_accessor :postalcode (:zipcode)
66
59
  def postalcode
67
60
  fetch_address && fetch_address["fetch_zipcode"] || zipcode
@@ -70,7 +63,6 @@ class MagicAddresses::Address < ActiveRecord::Base
70
63
  self.fetch_address = (fetch_address || {}).merge("fetch_zipcode" => value)
71
64
  end
72
65
 
73
-
74
66
  %w[country state city district subdistrict].each do |key|
75
67
  # attr_accessor key
76
68
  define_method(key) do
@@ -8,32 +8,52 @@ module MagicAddresses
8
8
 
9
9
  ## C L A S S - M E T H O D S # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
10
10
  module ClassMethods
11
-
11
+
12
+
12
13
  def has_addresses
13
- # => send :include, InstanceMethods
14
- has_many :addresses, as: :owner, class_name: "MagicAddresses::Address", dependent: :destroy
14
+ has_many :addresses,
15
+ as: :owner,
16
+ class_name: "MagicAddresses::Address",
17
+ dependent: :destroy
15
18
  accepts_nested_attributes_for :addresses, allow_destroy: true, reject_if: :all_blank
16
19
  end
17
20
 
21
+
18
22
  def has_one_address
19
23
  has_one :address, -> { where(default: true) },
20
- :as => :owner,
21
- :class_name => "MagicAddresses::Address",
22
- :autosave => true,
23
- :dependent => :destroy
24
- # => delegate :street, :street_number, :zipcode, :city, :district, :subdistrict, :state, :country,
25
- # => to: :address, allow_nil: true
26
- accepts_nested_attributes_for :address, allow_destroy: true # => , reject_if: :all_blank
24
+ as: :owner,
25
+ class_name: "MagicAddresses::Address",
26
+ autosave: true,
27
+ dependent: :destroy
28
+ accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :all_blank
29
+ end
30
+
31
+
32
+ def has_nested_address
33
+ send :include, InstanceMethods
34
+ has_one :address,
35
+ as: :owner,
36
+ class_name: "MagicAddresses::Address",
37
+ autosave: true,
38
+ dependent: :destroy
39
+ delegate :street, :number, :postalcode, :city, :district, :subdistrict, :state, :country,
40
+ :street=, :number=, :postalcode=, :city=, :country=,
41
+ to: :address, allow_nil: true
42
+ accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :all_blank
43
+ alias_method_chain :address, :build
27
44
  end
28
-
29
-
45
+
46
+
30
47
  end #> ClassMethods
31
48
 
32
49
  ## I N S T A N C E - M E T H O D S # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
33
50
  module InstanceMethods
34
-
35
- # ...
36
-
51
+
52
+ # http://stackoverflow.com/a/4033761
53
+ def address_with_build
54
+ self.address_without_build || self.build_address
55
+ end
56
+
37
57
  end #> InstanceMethods
38
58
 
39
59
  end
@@ -4,6 +4,11 @@ class MagicAddresses::Country < ActiveRecord::Base
4
4
  # =====> A S S O Z I A T I O N S <========================================================= #
5
5
  has_many :addresses, class_name: "MagicAddresses::Address", foreign_key: :country_id
6
6
 
7
+ has_many :states, class_name: "MagicAddresses::State", foreign_key: :country_id
8
+ has_many :cities, through: :states, source: :cities
9
+ has_many :districts, through: :cities, source: :districts
10
+ has_many :subdistricts, through: :districts, source: :subdistricts
11
+
7
12
 
8
13
  # =====> A T T R I B U T E S <============================================================= #
9
14
  mgca_translate :name
@@ -6,6 +6,10 @@ class MagicAddresses::District < ActiveRecord::Base
6
6
 
7
7
  belongs_to :city, class_name: "MagicAddresses::City", foreign_key: :city_id
8
8
 
9
+ has_one :state, through: :city, source: :state
10
+ has_one :country, through: :state, source: :country
11
+
12
+ has_many :subdistricts, class_name: "MagicAddresses::District", foreign_key: :district_id
9
13
 
10
14
  # =====> A T T R I B U T E S <============================================================= #
11
15
  mgca_translate :name
@@ -6,6 +6,10 @@ class MagicAddresses::State < ActiveRecord::Base
6
6
 
7
7
  belongs_to :country, class_name: "MagicAddresses::Country", foreign_key: :country_id
8
8
 
9
+ has_many :cities, class_name: "MagicAddresses::City", foreign_key: :state_id
10
+ has_many :districts, through: :cities, source: :districts
11
+ has_many :subdistricts, through: :districts, source: :subdistricts
12
+
9
13
 
10
14
  # =====> A T T R I B U T E S <============================================================= #
11
15
  mgca_translate :name
@@ -7,6 +7,8 @@ class MagicAddresses::Subdistrict < ActiveRecord::Base
7
7
  belongs_to :city, class_name: "MagicAddresses::City", foreign_key: :city_id
8
8
  belongs_to :district, class_name: "MagicAddresses::District", foreign_key: :district_id
9
9
 
10
+ has_one :state, through: :city, source: :state
11
+ has_one :country, through: :state, source: :country
10
12
 
11
13
  # =====> A T T R I B U T E S <============================================================= #
12
14
  mgca_translate :name
@@ -3,8 +3,6 @@ class AddMagicAddresses < ActiveRecord::Migration
3
3
  def up
4
4
 
5
5
  # if using with postgresql
6
- # => # add hstore extensions
7
- # => enable_extension "hstore"
8
6
  # => # add extensions for distance calculation
9
7
  # => enable_extension "cube"
10
8
  # => enable_extension "earthdistance"
@@ -16,12 +14,7 @@ class AddMagicAddresses < ActiveRecord::Migration
16
14
 
17
15
  t.string :name # => name (if needed)
18
16
 
19
- # if not empty fetch and translate that address
20
- if MagicAddresses.configuration.hstore
21
- t.hstore :fetch_address # => hstore method
22
- else
23
- t.text :fetch_address # => default serialize field
24
- end
17
+ t.text :fetch_address # => default serialize field
25
18
 
26
19
  # t.string :street
27
20
  t.string :street_default
@@ -199,8 +192,6 @@ class AddMagicAddresses < ActiveRecord::Migration
199
192
  # => # disable extensions for distance calculation
200
193
  # => disable_extension "earthdistance"
201
194
  # => disable_extension "cube"
202
- # => # disable hstore extension
203
- # => disable_extension "hstore"
204
195
 
205
196
  end
206
197
  end
@@ -9,6 +9,4 @@ MagicAddresses.configure do |config|
9
9
  config.job_backend = :none
10
10
  # use a postgres earthdistance for distance calculation
11
11
  config.earthdistance = false
12
- # use a postgres hstore or serializer
13
- config.hstore = false
14
12
  end
@@ -2,18 +2,11 @@ require "action_view/helpers/asset_tag_helper"
2
2
 
3
3
  # encoding: utf-8
4
4
  module MgcaHelper
5
-
6
- def address_fields_for( fff, options = {} )
7
- options.merge({f: fff})
8
- return render partial: "magic_addresses/addresses/fields/address", locals: options
9
- end
10
-
11
-
12
- def country_flag( lcl = I18n.locale.to_s, format = "small" )
13
- format = "medium" unless %w(small medium large).include?(format)
14
- lcl = "xx" unless %w(at by be ba bg ca hr cy cz dk fi fr de gr hu ie it lv li lt lu nl no pl pt ro ru rs sk si es se ch ua gb us).include?( lcl.to_s.downcase )
15
- raw( image_tag( "flags/countries/#{format}/flag-#{lcl.to_s.downcase}.png", class: "flag" ) )
16
- end
17
-
5
+
6
+ def country_flag( lcl = I18n.locale.to_s, format = "small" )
7
+ format = "medium" unless %w(small medium large).include?(format)
8
+ lcl = "xx" unless %w(at by be ba bg ca hr cy cz dk fi fr de gr hu ie it lv li lt lu nl no pl pt ro ru rs sk si es se ch ua gb us).include?( lcl.to_s.downcase )
9
+ raw( image_tag( "flags/countries/#{format}/flag-#{lcl.to_s.downcase}.png", class: "flag" ) )
10
+ end
18
11
 
19
12
  end
@@ -14,7 +14,6 @@ module MagicAddresses
14
14
  attr_accessor :job_backend
15
15
 
16
16
  attr_accessor :earthdistance
17
- attr_accessor :hstore
18
17
 
19
18
  def initialize
20
19
  @active_locales = [:en, :de]
@@ -22,7 +21,6 @@ module MagicAddresses
22
21
  @default_country = "Germany"
23
22
  @job_backend = :none
24
23
  @earthdistance = false
25
- @hstore = false
26
24
  end
27
25
 
28
26
  # Returns a hash of all configurable options
@@ -1,3 +1,3 @@
1
1
  module MagicAddresses
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -9,6 +9,4 @@ MagicAddresses.configure do |config|
9
9
  config.job_backend = :none
10
10
  # use a postgres earthdistance for distance calculation
11
11
  config.earthdistance = false
12
- # use a postgres hstore or serializer
13
- config.hstore = false
14
12
  end
Binary file
@@ -3,8 +3,6 @@ class AddMagicAddresses < ActiveRecord::Migration
3
3
  def up
4
4
 
5
5
  # if using with postgresql
6
- # => # add hstore extensions
7
- # => enable_extension "hstore"
8
6
  # => # add extensions for distance calculation
9
7
  # => enable_extension "cube"
10
8
  # => enable_extension "earthdistance"
@@ -16,12 +14,7 @@ class AddMagicAddresses < ActiveRecord::Migration
16
14
 
17
15
  t.string :name # => name (if needed)
18
16
 
19
- # if not empty fetch and translate that address
20
- if MagicAddresses.configuration.hstore
21
- t.hstore :fetch_address # => hstore method
22
- else
23
- t.text :fetch_address # => default serialize field
24
- end
17
+ t.text :fetch_address # => default serialize field
25
18
 
26
19
  # t.string :street
27
20
  t.string :street_default
@@ -199,8 +192,6 @@ class AddMagicAddresses < ActiveRecord::Migration
199
192
  # => # disable extensions for distance calculation
200
193
  # => disable_extension "earthdistance"
201
194
  # => disable_extension "cube"
202
- # => # disable hstore extension
203
- # => disable_extension "hstore"
204
195
 
205
196
  end
206
197
  end
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20150223223200) do
14
+ ActiveRecord::Schema.define(version: 20150225220219) do
15
15
 
16
16
  create_table "mgca_address_translations", force: :cascade do |t|
17
17
  t.integer "mgca_address_id", null: false
Binary file