g5_updatable 0.20.3.pre.1 → 1.0.2.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +48 -20
  3. data/Rakefile +30 -5
  4. data/app/concerns/g5_updatable/first_class_properties.rb +1 -1
  5. data/app/controllers/g5_updatable/base_updatable_controller.rb +0 -2
  6. data/app/controllers/g5_updatable/feed_controller.rb +1 -1
  7. data/app/controllers/g5_updatable/syncs_controller.rb +2 -2
  8. data/{lib → app/lib}/g5_updatable/all_client_urns_fetcher.rb +2 -1
  9. data/{lib → app/lib}/g5_updatable/client_feed_processor.rb +4 -2
  10. data/{lib → app/lib}/g5_updatable/client_updater.rb +0 -0
  11. data/{lib → app/lib}/g5_updatable/fetcher.rb +12 -3
  12. data/{lib → app/lib}/g5_updatable/indifferentizer.rb +0 -0
  13. data/{lib → app/lib}/g5_updatable/locations_updater.rb +42 -1
  14. data/app/lib/g5_updatable/version.rb +5 -0
  15. data/app/models/g5_updatable/client.rb +11 -0
  16. data/app/models/g5_updatable/hour_set.rb +6 -0
  17. data/app/models/g5_updatable/hub_amenity.rb +15 -10
  18. data/app/models/g5_updatable/location.rb +22 -10
  19. data/app/models/g5_updatable/point_of_interest.rb +6 -0
  20. data/app/models/g5_updatable/special_date.rb +16 -0
  21. data/app/models/g5_updatable/week_day.rb +6 -0
  22. data/app/workers/g5_updatable/client_feed_processor_worker.rb +11 -0
  23. data/config/initializers/inflections.rb +16 -0
  24. data/config/initializers/sidekiq.rb +7 -0
  25. data/db/migrate/20140709222005_create_g5_updatable_clients_and_locations.rb +1 -1
  26. data/db/migrate/20141030211945_create_integration_setting.rb +1 -1
  27. data/db/migrate/20141122211945_remove_integration_setting.rb +1 -1
  28. data/db/migrate/20141211211945_add_name_to_clients_and_locations.rb +1 -1
  29. data/db/migrate/20141211711945_update_names.rb +1 -1
  30. data/db/migrate/20151103043916_add_latitude_and_longitude_to_location.rb +1 -1
  31. data/db/migrate/20151103050229_copy_lat_long_props_to_lat_long_columns.rb +1 -1
  32. data/db/migrate/20151106070749_add_latitude_longitude_indexes_to_location.rb +1 -1
  33. data/db/migrate/20161122070749_add_amenities.rb +1 -1
  34. data/db/migrate/20161209070749_add_client_urn_to_locations.rb +1 -1
  35. data/db/migrate/20170422000000_add_points_of_interest.rb +20 -0
  36. data/db/migrate/20170428000000_add_hour_sets.rb +44 -0
  37. data/db/migrate/20170507000000_add_unique_urn_indexes.rb +9 -0
  38. data/db/migrate/20170720000000_add_display_name_to_locations.rb +6 -0
  39. data/db/migrate/20170804184206_add_details_to_points_of_interest.rb +7 -0
  40. data/db/migrate/20180213184549_change_fields_for_week_days_and_special_dates.rb +9 -0
  41. data/db/migrate/20180220203057_add_indexes_to_week_days.rb +7 -0
  42. data/db/migrate/20180725125651_add_location_id_to_special_dates.rb +7 -0
  43. data/lib/g5_updatable/engine.rb +18 -0
  44. data/lib/g5_updatable/factories.rb +17 -1
  45. data/lib/g5_updatable/rspec.rb +1 -1
  46. data/lib/g5_updatable/rspec/factories.rb +37 -1
  47. data/lib/g5_updatable/version.rb +3 -1
  48. data/lib/tasks/g5_updatable_tasks.rake +3 -3
  49. data/spec/concerns/g5_updatable/belongs_to_client_spec.rb +3 -1
  50. data/spec/concerns/g5_updatable/belongs_to_location_spec.rb +6 -5
  51. data/spec/controllers/feed_controller_spec.rb +11 -12
  52. data/spec/controllers/locations_controller_spec.rb +29 -5
  53. data/spec/controllers/syncs_controller_spec.rb +27 -19
  54. data/spec/dummy/config/environments/test.rb +18 -3
  55. data/spec/dummy/db/migrate/20140630175259_create_locations.rb +1 -1
  56. data/spec/dummy/db/migrate/20140630175330_create_clients.rb +1 -1
  57. data/spec/dummy/db/migrate/20140709220627_drop_clients_and_locations.rb +1 -1
  58. data/spec/dummy/db/migrate/20140714225203_create_favorite_foods.rb +1 -1
  59. data/spec/dummy/db/migrate/20141222072623_create_restaurants.rb +1 -1
  60. data/spec/dummy/db/schema.rb +99 -41
  61. data/spec/dummy/log/development.log +311 -0
  62. data/spec/dummy/log/test.log +113030 -0
  63. data/spec/fixtures/mj-cl-1tj7razq-the-commons-at-pilot-butte.json +231 -0
  64. data/spec/fixtures/mj-cl-1tj7razq-the-commons-at-pilot-butte2.json +193 -0
  65. data/spec/lib/g5_updatable/all_client_urns_fetcher_spec.rb +3 -3
  66. data/spec/lib/g5_updatable/client_feed_processor_spec.rb +44 -5
  67. data/spec/lib/g5_updatable/client_updater_spec.rb +2 -2
  68. data/spec/lib/g5_updatable/fetcher_spec.rb +28 -0
  69. data/spec/lib/g5_updatable/locations_updater_spec.rb +69 -11
  70. data/spec/models/g5_updatable/client_spec.rb +38 -0
  71. data/spec/models/g5_updatable/hour_set_spec.rb +12 -0
  72. data/spec/models/g5_updatable/location_spec.rb +46 -11
  73. data/spec/models/g5_updatable/point_of_interest_spec.rb +20 -0
  74. data/spec/models/g5_updatable/special_date_spec.rb +10 -0
  75. data/spec/models/g5_updatable/week_day_spec.rb +9 -0
  76. data/spec/serializers/g5_updatable/location_serializer_spec.rb +1 -1
  77. data/spec/spec_helper.rb +35 -10
  78. data/spec/support/safe_request_helpers.rb +26 -0
  79. data/spec/support/shared_example_for_urn_as_parameter.rb +1 -1
  80. data/spec/support/shared_examples_for_first_class_properties_json.rb +1 -1
  81. data/spec/workers/g5_updatable/client_feed_processor_worker_spec.rb +39 -0
  82. metadata +121 -74
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fff24c03cbb209168e956147a0266737b9bcfba9
4
- data.tar.gz: b459baf96623cb716ed624e606307f1712359d40
3
+ metadata.gz: 93beab4174e49a498235996df13b1bc146415102
4
+ data.tar.gz: 4a7f6ceff1db6fb48aa3182b4504f78bfed7603f
5
5
  SHA512:
6
- metadata.gz: fccf2277a17f67d1c7945ef14c90084b60eb11d42e5937352b4cefe52b45fa79d704f857329593df4bddf78a3571d0987b4b731bf0424d1e30c1e7b1ce35e825
7
- data.tar.gz: c550dc2f70ec537c76570849bc2f54b637367b5ad5c9c55bf59dae1bae1d1d2acf99adb11b97ef4d97b70993a50ebd74829106e3e7028056be686db8af4aac0c
6
+ metadata.gz: efe5b305ac002832bcf559a2626da2dbd830d2221c52c5442bd993cd70787e7a2ad3ebea0f6691aa46e30f1585fa7e5eb1ae7c9c5389b16eee2e8dd61052eba0
7
+ data.tar.gz: e4b950c357105f6d4c008451ecab304fdb80cc0b8f700f2e917bd7d0635b239dffecba04adf47762c8a342d5dd8dd852e714346b58f53d6351e01fb3e110241e
data/README.md CHANGED
@@ -1,14 +1,16 @@
1
1
  # G5 Updatable
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/g5_updatable.svg)](http://badge.fury.io/rb/g5_updatable)
4
- [![Build Status](https://travis-ci.org/G5/g5_updatable.png?branch=master)](https://travis-ci.org/G5/g5_updatable)
5
-
6
3
  G5 Updatable provides a solution for automatic updates of client and location
7
4
  data when modified or created in the G5 Hub.
8
5
 
9
6
  ## Requirements
10
7
 
11
- G5 Updatable makes use of PostgrSQL's `json` field type, and so only supports implementing apps that also use PostgreSQL.
8
+ G5 Updatable makes use of PostgreSQL's `json` field type, and so only supports
9
+ apps that also use PostgreSQL.
10
+
11
+ Compatible with rails 4.x : Use version 0.x
12
+
13
+ Compatible with rails 5.x : Use version >= 1.0.0
12
14
 
13
15
  ## Installation
14
16
 
@@ -18,12 +20,13 @@ G5 Updatable makes use of PostgrSQL's `json` field type, and so only supports im
18
20
  gem 'g5_updatable'
19
21
  ```
20
22
 
21
- 2. And then execute:
23
+ 1. And then execute:
22
24
 
23
25
  ```console
24
26
  bundle
25
27
  ```
26
- 3. Run the generator.
28
+
29
+ 1. Run the generator.
27
30
 
28
31
  ```ruby
29
32
  rails g g5_updatable:install
@@ -31,12 +34,19 @@ G5 Updatable makes use of PostgrSQL's `json` field type, and so only supports im
31
34
 
32
35
  This mounts the engine at `/g5_updatable`.
33
36
 
34
- 3. And copy the engine's migrations to your application:
37
+ 1. And copy the engine's migrations to your application:
35
38
 
36
- ```console
39
+ ```console
37
40
  rake g5_updatable:install:migrations
38
- ```
39
- 3. Optional: load all of G5-Hub's data into your database
41
+ ```
42
+ 1. Run the schema AND data migrations:
43
+
44
+ ```console
45
+ rake db:migrate
46
+ rake g5_updatable:data_migration
47
+ ```
48
+
49
+ 2. Optional: load all of G5-Hub's data into your database
40
50
 
41
51
  **Important:** The entries.js and entries.json endpoint on the hub are deprecated. So, for all versions of g5_updatable before 0.17.0, this task will only load the newest 10 clients. If you would like to load all clients, please specify "~> 0.17.0" or higher in your Gemfile.
42
52
 
@@ -79,6 +89,7 @@ end
79
89
 
80
90
  Also optionally expect a secondary block argument, receiving the object previous to be updated
81
91
  ... you know.. For diff reasons.
92
+
82
93
  ```ruby
83
94
  G5Updatable::LocationsUpdater.on_update do |g5_updatable_location, before_update_location|
84
95
  puts "G5Updatable::Location##{before_update_location.properties} changed to "
@@ -110,6 +121,11 @@ end
110
121
 
111
122
  It provides a `#location` method that will fetch the correct location.
112
123
 
124
+ ### Dependencies
125
+
126
+ Versions >= 0.28.0 include Sidekiq as a dependency so that feed processing can be
127
+ handled asynchronously instead of in the context of a web request. Note that we assume a queue of ```g5_updatable``` is present and that applications with custom queues should make sure to include one by that name, which can be prioritized per app as needed.
128
+
113
129
  ### Spec Helpers
114
130
 
115
131
  The engine provides a helper files that can be included in your project to bring in some testing support. Currently this is limited to (some factory definitions)[https://github.com/G5/g5_updatable/blob/active-record-up-in-here/lib/g5_updatable/rspec/factories.rb]. In rspec you can add the following line to your `spec/spec_helper.rb`:
@@ -118,9 +134,9 @@ The engine provides a helper files that can be included in your project to bring
118
134
  require 'g5_updatable/rspec'
119
135
  ```
120
136
 
121
- ### FactoryGirl Factories
137
+ ### FactoryBot Factories
122
138
 
123
- For easier testing, if your app uses `FactoryGirl`, you can get access to factories by `require "g5_updatable/factories"`.
139
+ For easier testing, if your app uses `FactoryBot`, you can get access to factories by `require "g5_updatable/factories"`.
124
140
 
125
141
  ## Authors
126
142
 
@@ -143,17 +159,29 @@ If you find bugs, have feature requests or questions, please
143
159
  ## Specs
144
160
 
145
161
  The `database.yml` for the dummy app must be created and modified to match your
146
- PostgreSQL configuration. If you are using the [G5 Orion
147
- Vagrant](https://github.com/G5/g5-orion-vagrant) image, the sample file should
148
- just work. You can copy it into place with:
149
- ```bash
150
- $ cp spec/dummy/config/database.sample.yml spec/dummy/config/database.yml
162
+ PostgreSQL configuration. You can copy it into place with:
163
+
164
+ ```console
165
+ cp spec/dummy/config/database.sample.yml spec/dummy/config/database.yml
166
+ ```
167
+
168
+ Set up the test database for the first time:
169
+
170
+ ```console
171
+ bundle exec rake db:test:setup
172
+ ```
173
+
174
+ Run specs:
175
+
176
+ ```console
177
+ bundle exec rspec spec
151
178
  ```
152
179
 
153
- Run specs via `rspec` with:
180
+ If you add a migration, make sure you apply it to the dummy app and commit the
181
+ changes to spec/dummy/db/schema.rb:
154
182
 
155
- ```bash
156
- $ rspec spec
183
+ ```console
184
+ bundle exec rake app:db:migrate RAILS_ENV=test
157
185
  ```
158
186
 
159
187
  ## License
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'bundler/setup'
3
5
  rescue LoadError
@@ -14,7 +16,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
16
  rdoc.rdoc_files.include('lib/**/*.rb')
15
17
  end
16
18
 
17
- APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
19
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
18
20
  load 'rails/tasks/engine.rake'
19
21
 
20
22
  Bundler::GemHelper.install_tasks
@@ -22,9 +24,32 @@ Bundler::GemHelper.install_tasks
22
24
  require 'rspec/core'
23
25
  require 'rspec/core/rake_task'
24
26
 
25
- Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
27
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each { |f| load f }
28
+
29
+ namespace :db do
30
+ namespace :test do
31
+ desc 'Set up a new test database'
32
+ task :setup do
33
+ Rails.env = 'test'
34
+ Rake::Task['app:db:create'].invoke
35
+ Rake::Task['app:db:migrate'].invoke
36
+ end
37
+
38
+ desc 'Drop the test database'
39
+ task :drop do
40
+ Rails.env = 'test'
41
+ Rake::Task['app:db:drop'].invoke
42
+ end
43
+ end
44
+ end
26
45
 
27
- desc "Run all specs in spec directory (excluding plugin specs)"
46
+ desc 'Set up test db and run all specs'
47
+ RSpec::Core::RakeTask.new(spec: 'db:test:setup')
48
+ task default: :spec
28
49
 
29
- RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
30
- task :default => :spec
50
+ namespace :spec do
51
+ desc 'Run tests and clean up the db afterwards'
52
+ task clean: :spec do
53
+ Rake::Task['db:test:drop'].invoke
54
+ end
55
+ end
@@ -13,7 +13,7 @@ module G5Updatable::FirstClassProperties
13
13
  protected
14
14
 
15
15
  def define_methods_for_properties
16
- return unless properties.present?
16
+ return unless has_attribute?(:properties) && properties.present?
17
17
 
18
18
  properties.each do |key, value|
19
19
  next if respond_to?(key)
@@ -1,6 +1,4 @@
1
1
  module G5Updatable
2
2
  class BaseUpdatableController < ActionController::Base
3
- skip_before_filter :authenticate_user!
4
- skip_before_filter :verify_authenticity_token
5
3
  end
6
4
  end
@@ -1,6 +1,6 @@
1
1
  class G5Updatable::FeedController < G5Updatable::BaseUpdatableController
2
2
  def update
3
- G5Updatable::ClientFeedProcessor.new(params).work
3
+ G5Updatable::ClientFeedProcessorWorker.perform_async(client_uid: params[:client_uid], location_uid: params[:location_uid])
4
4
  render json: {}, status: :ok
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  class G5Updatable::SyncsController < G5Updatable::BaseUpdatableController
2
2
 
3
- before_filter :set_client
3
+ before_action :set_client
4
4
 
5
5
  def index
6
6
  timestamp = @client.locations.max_updated_at
@@ -16,4 +16,4 @@ class G5Updatable::SyncsController < G5Updatable::BaseUpdatableController
16
16
  def formatted_timestamp(timestamp)
17
17
  timestamp.in_time_zone.strftime("%I:%M%P on %B %e, %Y") if timestamp
18
18
  end
19
- end
19
+ end
@@ -1,7 +1,8 @@
1
1
  module G5Updatable
2
2
  class AllClientUrnsFetcher
3
3
  def self.fetch_uids
4
- G5Updatable::Fetcher.get_with_token(url)['clients'].collect do |urn|
4
+ return unless urns = G5Updatable::Fetcher.get_with_token(url).try(:[], 'clients')
5
+ urns.collect do |urn|
5
6
  "#{hub_url}/clients/#{urn['urn']}.json"
6
7
  end
7
8
  end
@@ -32,11 +32,13 @@ class G5Updatable::ClientFeedProcessor
32
32
  private
33
33
 
34
34
  def update_location
35
- G5Updatable::LocationsUpdater.new(location_hash).update
35
+ return unless update_params = location_hash
36
+ G5Updatable::LocationsUpdater.new(update_params).update
36
37
  end
37
38
 
38
39
  def update_client
39
- G5Updatable::ClientUpdater.new(client_hash).update
40
+ return unless update_params = client_hash
41
+ G5Updatable::ClientUpdater.new(update_params).update
40
42
  end
41
43
 
42
44
  def client_hash
File without changes
@@ -3,8 +3,11 @@ module G5Updatable
3
3
  extend ::G5AuthenticationClient::AuthTokenHelper
4
4
 
5
5
  def self.get_with_token(url)
6
+ url = self.url_for_env(url)
6
7
  response = do_with_username_pw_access_token do |access_token|
7
- ::HTTParty.get(url, { query: { access_token: access_token },
8
+ query = { access_token: access_token, updatable_version: G5Updatable::VERSION }
9
+ query[:updatable_referer] = ENV['HEROKU_APP_NAME'] unless ENV['HEROKU_APP_NAME'].blank?
10
+ ::HTTParty.get(url, { query: query,
8
11
  headers: { 'Content-Type' => 'application/json',
9
12
  'Accept' => 'application/json' } })
10
13
  end
@@ -13,10 +16,16 @@ module G5Updatable
13
16
  when 200
14
17
  JSON.parse(response.body)
15
18
  when 404
16
- raise "Couldn't find record at URL '#{url}'"
19
+ Rails.logger.warn "Couldn't find record at URL '#{url}'"
20
+ return {}
17
21
  else
18
22
  raise "I got an unexpected response code '#{response.code}'"
19
23
  end
20
24
  end
25
+
26
+ def self.url_for_env(url)
27
+ return url.gsub(/(http|https):\/\/([a-zA-Z0-9-]*\.)+[a-zA-Z0-9]+/, ENV['HUB_URL']) if ENV['HUB_URL'].present?
28
+ url
29
+ end
21
30
  end
22
- end
31
+ end
File without changes
@@ -39,7 +39,10 @@ class G5Updatable::LocationsUpdater
39
39
  before_update_object = location.dup
40
40
  is_new_loc = location.new_record?
41
41
 
42
- amenities = attributes.delete(:amenities)
42
+ amenities = attributes.delete(:amenities)
43
+ points_of_interest = attributes.delete(:points_of_interest)
44
+ hour_sets = attributes.delete(:hour_sets)
45
+ special_dates = attributes.delete(:special_dates)
43
46
  location.update_attributes!(
44
47
  uid: attributes[:uid],
45
48
  name: attributes[:name],
@@ -50,6 +53,9 @@ class G5Updatable::LocationsUpdater
50
53
  updated_at: Time.now
51
54
  )
52
55
  create_amenities(amenities, location)
56
+ create_points_of_interest(points_of_interest, location)
57
+ create_hour_sets(hour_sets, location)
58
+ create_special_dates(special_dates, location)
53
59
  self.class.on_create_callbacks.each { |cb| cb.call(location) } if is_new_loc
54
60
  self.class.on_update_callbacks.each { |cb| cb.call(location, before_update_object) }
55
61
  end
@@ -63,6 +69,41 @@ class G5Updatable::LocationsUpdater
63
69
  location.refresh_flat_amenity_names!
64
70
  end
65
71
 
72
+ def create_points_of_interest(poi_hashes, location)
73
+ location.points_of_interest.delete_all
74
+ return if poi_hashes.blank?
75
+ poi_hashes.each do |poi_hash|
76
+ location.points_of_interest.create(poi_hash.except(:id, :location_id))
77
+ end
78
+ end
79
+
80
+ def create_hour_sets(hour_set_hashes, location)
81
+ location.hour_sets.destroy_all
82
+ return if hour_set_hashes.blank?
83
+ hour_set_hashes.each do |hour_set_hash|
84
+ attributes = hour_set_hash.dup
85
+ week_days = attributes.delete(:week_days)
86
+ special_dates = attributes.delete(:special_dates)
87
+ hour_set = location.hour_sets.create(attributes.except(:id, :location_id))
88
+ create_week_days(week_days, hour_set)
89
+ end
90
+ end
91
+
92
+ def create_week_days(week_day_hashes, hour_set)
93
+ return if week_day_hashes.blank?
94
+ week_day_hashes.each do |week_day_hash|
95
+ hour_set.week_days.create(week_day_hash.except(:id, :hour_set_id))
96
+ end
97
+ end
98
+
99
+ def create_special_dates(special_date_hashes, location)
100
+ location.special_dates.destroy_all
101
+ return if special_date_hashes.blank?
102
+ special_date_hashes.each do |special_date_hash|
103
+ location.special_dates.create(special_date_hash.except(:id))
104
+ end
105
+ end
106
+
66
107
  def create_or_update_amenity(amenity_hash)
67
108
  amenity = G5Updatable::HubAmenity.where(external_id: amenity_hash[:id]).first_or_create
68
109
  if amenity.name != amenity_hash[:name] || amenity.icon != amenity_hash[:icon]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module G5Updatable
4
+ VERSION = '1.0.1'
5
+ end
@@ -10,5 +10,16 @@ module G5Updatable
10
10
  })
11
11
 
12
12
  validates :uid, :urn, presence: true, uniqueness: true
13
+
14
+ CENTRALIZED_CLS_URL_PROPERTY = 'centralized_cls_url'.freeze
15
+ CENTRALIZED_CMS_URL_PROPERTY = 'centralized_cms_url'.freeze
16
+
17
+ def centralized_cls?
18
+ properties[CENTRALIZED_CLS_URL_PROPERTY].present?
19
+ end
20
+
21
+ def centralized_cms?
22
+ properties[CENTRALIZED_CMS_URL_PROPERTY].present?
23
+ end
13
24
  end
14
25
  end
@@ -0,0 +1,6 @@
1
+ module G5Updatable
2
+ class HourSet < ActiveRecord::Base
3
+ belongs_to :location, foreign_key: :g5_updatable_location_id
4
+ has_many :week_days, dependent: :destroy
5
+ end
6
+ end
@@ -1,21 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module G5Updatable
2
4
  class HubAmenity < ActiveRecord::Base
3
5
  validates :external_id, presence: true
4
6
  has_many :hub_amenities_locations, foreign_key: :g5_updatable_hub_amenity_id
5
7
 
6
- scope :by_client_urn, -> (client_urn) {
7
- joins(hub_amenities_locations: { location: :client }).
8
- where('g5_updatable_clients.urn = :client_urn', client_urn: client_urn).uniq
8
+ scope :by_client_urn, lambda { |client_urn|
9
+ joins(hub_amenities_locations: { location: :client })
10
+ .where('g5_updatable_clients.urn = :client_urn',
11
+ client_urn: client_urn).distinct
9
12
  }
10
13
 
11
- scope :by_client_urns, -> (client_urns) {
12
- joins(hub_amenities_locations: { location: :client }).
13
- where('g5_updatable_clients.urn IN (:client_urns)', client_urns: client_urns.join(',')).uniq
14
+ scope :by_client_urns, lambda { |client_urns|
15
+ joins(hub_amenities_locations: { location: :client })
16
+ .where('g5_updatable_clients.urn IN (:client_urns)',
17
+ client_urns: client_urns.join(',')).distinct
14
18
  }
15
19
 
16
- scope :by_client_uid, -> (client_uid) {
17
- joins(hub_amenities_locations: :location).
18
- where('g5_updatable_locations.client_uid = :client_uid', client_uid: client_uid).uniq
20
+ scope :by_client_uid, lambda { |client_uid|
21
+ joins(hub_amenities_locations: :location)
22
+ .where('g5_updatable_locations.client_uid = :client_uid',
23
+ client_uid: client_uid).distinct
19
24
  }
20
25
  end
21
- end
26
+ end
@@ -4,11 +4,17 @@ module G5Updatable
4
4
  include G5Updatable::UrnAsParameter
5
5
  include BelongsToClient
6
6
 
7
+ has_many :points_of_interest, foreign_key: :g5_updatable_location_id, dependent: :destroy
8
+ has_many :hour_sets, foreign_key: :g5_updatable_location_id, dependent: :destroy
9
+ has_many :week_days, through: :hour_sets, dependent: :destroy
10
+ has_many :special_dates, foreign_key: :g5_updatable_location_id, dependent: :destroy
7
11
  has_many :hub_amenities_locations, foreign_key: :g5_updatable_location_id, dependent: :destroy
8
12
  has_many :hub_amenities, through: :hub_amenities_locations
9
13
 
10
- validates :uid, :urn, :client_uid, :client_urn, presence: true
14
+ validates :uid, :urn, :client_uid, presence: true
11
15
  validates :urn, uniqueness: true
16
+ # Only validate if the column exists, which it doesn't on older versions of this gem
17
+ validates :client_urn, presence: true, if: Proc.new { |l| l.respond_to? :client_urn }
12
18
 
13
19
  scope :by_client_uid, -> (client_uid) { where(client_uid: client_uid) }
14
20
  scope :by_client_urn, -> (client_urn) { where(client_urn: client_urn) }
@@ -30,7 +36,11 @@ module G5Updatable
30
36
  end
31
37
  results
32
38
  }
33
- before_validation :set_client_urn, :set_client_uid
39
+ before_validation :set_client_urn, :set_client_uid, :set_display_name
40
+
41
+ def self.ordered_display_names_and_urns_by_client_urn(client_urn)
42
+ where(client_urn: client_urn).order(:display_name).select(:display_name, :urn)
43
+ end
34
44
 
35
45
  def refresh_flat_amenity_names!
36
46
  update_attributes(flat_amenity_names: self.class.amenity_names_to_tokenized_string(hub_amenities.collect(&:name)))
@@ -47,12 +57,6 @@ module G5Updatable
47
57
  "#{FLAT_DELIM}#{[names_array].flatten.compact.sort.join(FLAT_DELIM)}#{FLAT_DELIM}"
48
58
  end
49
59
 
50
- def display_name
51
- return nil unless properties
52
- return properties['internal_branded_name'] if properties['internal_branded_name'].present?
53
- name
54
- end
55
-
56
60
  def neighborhoods
57
61
  [properties['neighborhood'], properties['neighborhood_2']].select(&:present?)
58
62
  end
@@ -62,7 +66,7 @@ module G5Updatable
62
66
  end
63
67
 
64
68
  def no_deploy?
65
- status?("No Deploy")
69
+ properties['no_deploy']
66
70
  end
67
71
 
68
72
  def pending?
@@ -83,12 +87,20 @@ module G5Updatable
83
87
  !!match
84
88
  end
85
89
 
90
+ # Custom setter because older version of this gem don't have this attrbute
86
91
  def set_client_urn
87
- self.client_urn ||= self.client_uid.split('/').last if self.client_uid
92
+ if self.client_uid and self.respond_to?(:client_urn)
93
+ self.client_urn ||= self.client_uid.split('/').last
94
+ end
88
95
  end
89
96
 
90
97
  def set_client_uid
91
98
  self.client_uid||= self.client.try(:uid)
92
99
  end
100
+
101
+ def set_display_name
102
+ branded = properties.try(:[], 'internal_branded_name')
103
+ self.display_name = branded.present? ? branded : name
104
+ end
93
105
  end
94
106
  end