diff options
| author | Alban Peignier | 2018-01-10 20:34:09 +0100 |
|---|---|---|
| committer | GitHub | 2018-01-10 20:34:09 +0100 |
| commit | 17a3d2437a4eb60ec4046137fb37d7b433dc48aa (patch) | |
| tree | 4e9f0df17da92ad00903bd851d81cdba6ac39282 | |
| parent | d597c065194e84f3c350c042276480361520b3b9 (diff) | |
| parent | 7a5e523886177de2cda3268effc9001e62246766 (diff) | |
| download | chouette-core-17a3d2437a4eb60ec4046137fb37d7b433dc48aa.tar.bz2 | |
Merge pull request #222 from af83/5505-custom_fields_with_jsonb
Custom fields with jsonb. Refs #5505
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 8 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 7 | ||||
| -rw-r--r-- | app/models/workgroup.rb | 2 | ||||
| -rw-r--r-- | config/initializers/apartment.rb | 3 | ||||
| -rw-r--r-- | db/migrate/20180109144120_create_custom_fields.rb | 14 | ||||
| -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 | 20 | ||||
| -rw-r--r-- | spec/factories/custom_fields.rb | 9 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 5 | ||||
| -rw-r--r-- | spec/models/custom_field_spec.rb | 27 |
11 files changed, 98 insertions, 7 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index d4dc82a56..11da77948 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/app/models/custom_field.rb b/app/models/custom_field.rb new file mode 100644 index 000000000..e8e76c6b5 --- /dev/null +++ b/app/models/custom_field.rb @@ -0,0 +1,7 @@ +class CustomField < ActiveRecord::Base + + extend Enumerize + enumerize :field_type, in: %i{list} + + validates :name, uniqueness: {scope: :resource_type} +end diff --git a/app/models/workgroup.rb b/app/models/workgroup.rb index bc2831bbd..995917fac 100644 --- a/app/models/workgroup.rb +++ b/app/models/workgroup.rb @@ -9,4 +9,6 @@ class Workgroup < ActiveRecord::Base validates_presence_of :line_referential_id validates_presence_of :stop_area_referential_id + + has_many :custom_fields end diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb index 2031d9918..2d06fb88b 100644 --- a/config/initializers/apartment.rb +++ b/config/initializers/apartment.rb @@ -79,7 +79,8 @@ Apartment.configure do |config| 'ComplianceCheckBlock', 'ComplianceCheckResource', 'ComplianceCheckMessage', - 'Merge' + 'Merge', + 'CustomField', ] # use postgres schemas? diff --git a/db/migrate/20180109144120_create_custom_fields.rb b/db/migrate/20180109144120_create_custom_fields.rb new file mode 100644 index 000000000..49df645c5 --- /dev/null +++ b/db/migrate/20180109144120_create_custom_fields.rb @@ -0,0 +1,14 @@ +class CreateCustomFields < ActiveRecord::Migration + def change + create_table :custom_fields do |t| + t.string :code + t.string :resource_type + t.string :name + t.string :field_type + t.json :options + t.bigint :workgroup_id + + t.timestamps null: false + end + end +end 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..873dc97d4 --- /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, :jsonb, default: {} + end +end diff --git a/db/schema.rb b/db/schema.rb index 19af8690b..f55800c8b 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: 20180109133022) do +ActiveRecord::Schema.define(version: 20180109180350) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -281,6 +281,19 @@ ActiveRecord::Schema.define(version: 20180109133022) do add_index "connection_links", ["objectid"], name: "connection_links_objectid_key", unique: true, using: :btree + create_table "custom_fields", id: :bigserial, force: :cascade do |t| + t.string "code" + t.string "resource_type" + t.string "name" + t.string "field_type" + t.json "options" + t.integer "workgroup_id", limit: 8 + t.datetime "created_at", null: false + 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" @@ -942,7 +955,7 @@ ActiveRecord::Schema.define(version: 20180109133022) do t.integer "route_id", limit: 8 t.integer "journey_pattern_id", limit: 8 t.integer "company_id", limit: 8 - t.string "objectid", null: false + t.string "objectid", null: false t.integer "object_version", limit: 8 t.string "comment" t.string "status_value" @@ -954,12 +967,13 @@ ActiveRecord::Schema.define(version: 20180109133022) do t.integer "number", limit: 8 t.boolean "mobility_restricted_suitability" t.boolean "flexible_service" - t.integer "journey_category", default: 0, null: false + t.integer "journey_category", default: 0, null: false t.datetime "created_at" t.datetime "updated_at" t.string "checksum" t.text "checksum_source" t.string "data_source_ref" + t.jsonb "custom_field_values", default: {} end add_index "vehicle_journeys", ["objectid"], name: "vehicle_journeys_objectid_key", unique: true, using: :btree diff --git a/spec/factories/custom_fields.rb b/spec/factories/custom_fields.rb new file mode 100644 index 000000000..2f5fae555 --- /dev/null +++ b/spec/factories/custom_fields.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :custom_field do + code "code" + resource_type "VehicleJourney" + sequence(:name){|n| "custom field ##{n}"} + field_type "list" + options( { "capacity" => "0" } ) + end +end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index eb2a31794..3ec2387e5 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -399,8 +399,7 @@ describe Chouette::VehicleJourney, :type => :model do end describe ".where_departure_time_between" do - it "selects vehicle journeys whose departure times are between the - specified range" do + it "selects vehicle journeys whose departure times are between the specified range" do journey_early = create( :vehicle_journey, stop_departure_time: '02:00:00' @@ -415,7 +414,7 @@ describe Chouette::VehicleJourney, :type => :model do journey_pattern: journey_pattern, stop_departure_time: '03:00:00' ) - journey_late = create( + create( :vehicle_journey, route: route, journey_pattern: journey_pattern, diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb new file mode 100644 index 000000000..80873683c --- /dev/null +++ b/spec/models/custom_field_spec.rb @@ -0,0 +1,27 @@ +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) } + end + + context "field access" do + let( :custom_field ){ build_stubbed :custom_field } + + 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 |
