Browse Source

Replace hardcoded address with hstore object

develop^2
Bèr Kessels 2 months ago
parent
commit
84032a0afe
4 changed files with 19 additions and 13 deletions
  1. 1
    0
      Rakefile
  2. 9
    2
      app/projections/nodes.rb
  3. 6
    11
      app/serializers/node.rb
  4. 3
    0
      config/event_sourcery.rb

+ 1
- 0
Rakefile View File

@@ -47,6 +47,7 @@ namespace :db do
47 47
       pg_db.disconnect
48 48
       app_db = Sequel.connect(Hours.config.database_url)
49 49
       app_db.run('CREATE EXTENSION postgis')
50
+      app_db.run('CREATE EXTENSION hstore')
50 51
     rescue Sequel::DatabaseError => e
51 52
       raise unless e.message.include?(
52 53
         "database \"#{ENV['DB_NAME']}\" already exists"

+ 9
- 2
app/projections/nodes.rb View File

@@ -16,11 +16,11 @@ module Hours
16 16
           column :name, :text
17 17
           column :opening_hours, :text
18 18
           column :location, 'geography(POINT)'
19
+          column :address, :hstore
19 20
         end
20 21
 
21 22
         # Event handlers that update the projection in response to different
22 23
         # events from the store.
23
-
24 24
         project NodeAdded do |event|
25 25
           table.insert(
26 26
             node_id: event.aggregate_id,
@@ -29,7 +29,14 @@ module Hours
29 29
             location: GeoRuby::SimpleFeatures::Point.from_x_y(
30 30
               event.body['lon'],
31 31
               event.body['lat']
32
-            )
32
+            ),
33
+            address: Sequel.hstore(
34
+              street: event.body['addr_street'],
35
+              housenumber: event.body['addr_housenumber'],
36
+              postcode: event.body['addr_postcode'],
37
+              city: event.body['addr_city'],
38
+              addr_country_code: event.body['addr_country_code']
39
+            ) # TODO: implement as Address Model PORO instead.
33 40
           )
34 41
         end
35 42
       end

+ 6
- 11
app/serializers/node.rb View File

@@ -11,8 +11,12 @@ module Hours
11 11
       id { @object.node_id }
12 12
 
13 13
       attribute :raw_opening_hours do
14
-        'Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30; '\
15
-         'PH closed; Su 12:00-17:00 open "Koopzondag"'
14
+        @object.opening_hours
15
+      end
16
+
17
+      attribute :address do
18
+        as_hash = Sequel::Postgres::HStore.parse(@object.address).to_hash
19
+        as_hash.slice('postcode', 'city', 'housenumber', 'street')
16 20
       end
17 21
 
18 22
       attribute :week_stable do
@@ -60,15 +64,6 @@ module Hours
60 64
         ]
61 65
       end
62 66
 
63
-      attribute :address do
64
-        {
65
-          postcode: '6511RA',
66
-          city: 'Nijmegen',
67
-          housenumber: '1',
68
-          street: 'Burchtstraat'
69
-        }
70
-      end
71
-
72 67
       link :self do
73 68
         # TODO: move  into an URL-helper
74 69
         "/nodes/#{@object.node_id}"

+ 3
- 0
config/event_sourcery.rb View File

@@ -6,6 +6,9 @@
6 6
 EventSourcery::Postgres.configure do |config|
7 7
   database = Sequel.connect(Hours.config.database_url)
8 8
 
9
+  Sequel.extension :pg_hstore
10
+  Sequel.extension :pg_hstore_ops
11
+
9 12
   # NOTE: Often we choose to split our events and projections into separate
10 13
   # databases. For the purposes of this example we'll use one.
11 14
   config.event_store_database = database

Loading…
Cancel
Save