diff options
| author | Robert | 2018-01-09 18:45:26 +0100 |
|---|---|---|
| committer | Alban Peignier | 2018-01-10 17:47:10 +0100 |
| commit | a7fe8da0bc230ee7f5d3173454fc07b596970d13 (patch) | |
| tree | 9b698c4d1d8c2bc1eecceaeed0f25ce76088392a | |
| parent | fd553851e9e6c59b998eef8577836a954c4d43f3 (diff) | |
| download | chouette-core-a7fe8da0bc230ee7f5d3173454fc07b596970d13.tar.bz2 | |
Fixes: #5505@2h; Migrations, specs and implementations
- Migration index_resource_type_on_custom_fields
- Specing behavior of custom field's resource (VehicleJourney)
- Migration create json col custom_field_values for resource (VehicleJourney)
- Create accessor methods inside resource (VehicleJourney)
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 8 | ||||
| -rw-r--r-- | config/initializers/apartment.rb | 4 | ||||
| -rw-r--r-- | db/migrate/20180109173815_add_index_resource_type_on_custom_fields.rb | 5 | ||||
| -rw-r--r-- | db/migrate/20180109180350_add_custom_field_values_to_vehicle_journeys.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 15 | ||||
| -rw-r--r-- | spec/models/custom_field_spec.rb | 10 |
6 files changed, 34 insertions, 13 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index d4dc82a56..89b230d85 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -243,6 +243,14 @@ module Chouette end end + def custom_fields + CustomField.where(resource_type: self.class.name.split("::").last) + end + + def custom_field_value key + custom_field_values[key.to_s] + end + def self.matrix(vehicle_journeys) Hash[*VehicleJourneyAtStop.where(vehicle_journey_id: vehicle_journeys.pluck(:id)).map do |vjas| [ "#{vjas.vehicle_journey_id}-#{vjas.stop_point_id}", vjas] diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb index 0f8d7c9de..2d06fb88b 100644 --- a/config/initializers/apartment.rb +++ b/config/initializers/apartment.rb @@ -37,7 +37,6 @@ Apartment.configure do |config| 'Chouette::GroupOfLine', 'Chouette::Company', 'Chouette::Network', - 'CustomField', 'ReferentialCloning', 'Workbench', 'Workgroup', @@ -80,7 +79,8 @@ Apartment.configure do |config| 'ComplianceCheckBlock', 'ComplianceCheckResource', 'ComplianceCheckMessage', - 'Merge' + 'Merge', + 'CustomField', ] # use postgres schemas? diff --git a/db/migrate/20180109173815_add_index_resource_type_on_custom_fields.rb b/db/migrate/20180109173815_add_index_resource_type_on_custom_fields.rb new file mode 100644 index 000000000..326e85806 --- /dev/null +++ b/db/migrate/20180109173815_add_index_resource_type_on_custom_fields.rb @@ -0,0 +1,5 @@ +class AddIndexResourceTypeOnCustomFields < ActiveRecord::Migration + def change + add_index :custom_fields, :resource_type + end +end diff --git a/db/migrate/20180109180350_add_custom_field_values_to_vehicle_journeys.rb b/db/migrate/20180109180350_add_custom_field_values_to_vehicle_journeys.rb new file mode 100644 index 000000000..3f0f7cd7b --- /dev/null +++ b/db/migrate/20180109180350_add_custom_field_values_to_vehicle_journeys.rb @@ -0,0 +1,5 @@ +class AddCustomFieldValuesToVehicleJourneys < ActiveRecord::Migration + def change + add_column :vehicle_journeys, :custom_field_values, :json + end +end diff --git a/db/schema.rb b/db/schema.rb index 99493260d..e415d4b83 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180109144120) do +ActiveRecord::Schema.define(version: 20180109180350) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -292,6 +292,8 @@ ActiveRecord::Schema.define(version: 20180109144120) do t.datetime "updated_at", null: false end + add_index "custom_fields", ["resource_type"], name: "index_custom_fields_on_resource_type", using: :btree + create_table "exports", id: :bigserial, force: :cascade do |t| t.integer "referential_id", limit: 8 t.string "status" @@ -971,6 +973,7 @@ ActiveRecord::Schema.define(version: 20180109144120) do t.string "checksum" t.text "checksum_source" t.string "data_source_ref" + t.json "custom_field_values" end add_index "vehicle_journeys", ["objectid"], name: "vehicle_journeys_objectid_key", unique: true, using: :btree @@ -996,21 +999,11 @@ ActiveRecord::Schema.define(version: 20180109144120) do t.integer "stop_area_referential_id", limit: 8 t.integer "output_id", limit: 8 t.string "objectid_format" - t.integer "workgroup_id", limit: 8 end add_index "workbenches", ["line_referential_id"], name: "index_workbenches_on_line_referential_id", using: :btree add_index "workbenches", ["organisation_id"], name: "index_workbenches_on_organisation_id", using: :btree add_index "workbenches", ["stop_area_referential_id"], name: "index_workbenches_on_stop_area_referential_id", using: :btree - add_index "workbenches", ["workgroup_id"], name: "index_workbenches_on_workgroup_id", using: :btree - - create_table "workgroups", id: :bigserial, force: :cascade do |t| - t.string "name" - t.integer "line_referential_id", limit: 8 - t.integer "stop_area_referential_id", limit: 8 - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end add_foreign_key "access_links", "access_points", name: "aclk_acpt_fkey" add_foreign_key "api_keys", "organisations" diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index b93d98187..80873683c 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -1,6 +1,7 @@ require 'rails_helper' RSpec.describe CustomField, type: :model do + let( :vj ){ create :vehicle_journey, custom_field_values: {energy: 99} } context "validates" do it { should validate_uniqueness_of(:name).scoped_to(:resource_type) } @@ -12,6 +13,15 @@ RSpec.describe CustomField, type: :model do it "option's values can be accessed by a key" do expect( custom_field.options['capacity'] ).to eq("0") end + end + + + context "custom fields for a resource" do + let!( :fields ){ (1..2).map{ create :custom_field } } + it { expect(vj.custom_fields).to eq(fields) } + end + context "custom field_values for a resource" do + it { expect(vj.custom_field_value("energy")).to eq(99) } end end |
