Browse Source

Replace hardcoded address with hstore object

tags/0.3.0^2
Bèr Kessels 10 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
pg_db.disconnect
app_db = Sequel.connect(Hours.config.database_url)
app_db.run('CREATE EXTENSION postgis')
app_db.run('CREATE EXTENSION hstore')
rescue Sequel::DatabaseError => e
raise unless e.message.include?(
"database \"#{ENV['DB_NAME']}\" already exists"

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

@@ -16,11 +16,11 @@ module Hours
column :name, :text
column :opening_hours, :text
column :location, 'geography(POINT)'
column :address, :hstore
end

# Event handlers that update the projection in response to different
# events from the store.

project NodeAdded do |event|
table.insert(
node_id: event.aggregate_id,
@@ -29,7 +29,14 @@ module Hours
location: GeoRuby::SimpleFeatures::Point.from_x_y(
event.body['lon'],
event.body['lat']
)
),
address: Sequel.hstore(
street: event.body['addr_street'],
housenumber: event.body['addr_housenumber'],
postcode: event.body['addr_postcode'],
city: event.body['addr_city'],
addr_country_code: event.body['addr_country_code']
) # TODO: implement as Address Model PORO instead.
)
end
end

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

@@ -11,8 +11,12 @@ module Hours
id { @object.node_id }

attribute :raw_opening_hours do
'Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30; '\
'PH closed; Su 12:00-17:00 open "Koopzondag"'
@object.opening_hours
end

attribute :address do
as_hash = Sequel::Postgres::HStore.parse(@object.address).to_hash
as_hash.slice('postcode', 'city', 'housenumber', 'street')
end

attribute :week_stable do
@@ -60,15 +64,6 @@ module Hours
]
end

attribute :address do
{
postcode: '6511RA',
city: 'Nijmegen',
housenumber: '1',
street: 'Burchtstraat'
}
end

link :self do
# TODO: move into an URL-helper
"/nodes/#{@object.node_id}"

+ 3
- 0
config/event_sourcery.rb View File

@@ -6,6 +6,9 @@
EventSourcery::Postgres.configure do |config|
database = Sequel.connect(Hours.config.database_url)

Sequel.extension :pg_hstore
Sequel.extension :pg_hstore_ops

# NOTE: Often we choose to split our events and projections into separate
# databases. For the purposes of this example we'll use one.
config.event_store_database = database

Loading…
Cancel
Save