diff options
| author | Zog | 2018-03-16 13:04:38 +0100 |
|---|---|---|
| committer | Johan Van Ryseghem | 2018-04-04 11:12:34 +0200 |
| commit | 6d15b7ec25592dc34cd95c738ef0854fdd1b94d2 (patch) | |
| tree | 29660f2f43956e39fe00ee2dadde4d7d234a4c56 | |
| parent | f0f619353c23775613fe154cb40d54cb55c979bb (diff) | |
| download | chouette-core-6d15b7ec25592dc34cd95c738ef0854fdd1b94d2.tar.bz2 | |
Refs #6196; Add Custom Fields to companies
| -rw-r--r-- | app/controllers/companies_controller.rb | 14 | ||||
| -rw-r--r-- | app/controllers/referential_companies_controller.rb | 11 | ||||
| -rw-r--r-- | app/models/chouette/company.rb | 2 | ||||
| -rw-r--r-- | app/models/concerns/custom_fields_support.rb | 12 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 15 | ||||
| -rw-r--r-- | app/views/companies/_form.html.slim | 4 | ||||
| -rw-r--r-- | app/views/companies/show.html.slim | 13 | ||||
| -rw-r--r-- | app/views/referential_companies/_form.html.slim | 37 | ||||
| -rw-r--r-- | app/views/referential_companies/edit.html.slim | 4 | ||||
| -rw-r--r-- | app/views/referential_companies/new.html.slim | 6 | ||||
| -rw-r--r-- | app/views/referential_companies/show.html.slim | 13 | ||||
| -rw-r--r-- | db/migrate/20180316115003_add_custom_field_values_to_companies.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 2 |
13 files changed, 88 insertions, 50 deletions
diff --git a/app/controllers/companies_controller.rb b/app/controllers/companies_controller.rb index 4afd12be1..a09cab783 100644 --- a/app/controllers/companies_controller.rb +++ b/app/controllers/companies_controller.rb @@ -38,12 +38,14 @@ class CompaniesController < ChouetteController protected def collection - @q = line_referential.companies.search(params[:q]) - + scope = line_referential.companies + @q = scope.search(params[:q]) + ids = @q.result(:distinct => true).pluck(:id) + scope = scope.where(id: ids) if sort_column && sort_direction - @companies ||= @q.result(:distinct => true).order(sort_column + ' ' + sort_direction).paginate(:page => params[:page]) + @companies ||= scope.order(sort_column + ' ' + sort_direction).paginate(:page => params[:page]) else - @companies ||= @q.result(:distinct => true).order(:name).paginate(:page => params[:page]) + @companies ||= scope.order(:name).paginate(:page => params[:page]) end end @@ -69,7 +71,9 @@ class CompaniesController < ChouetteController end def company_params - params.require(:company).permit( :objectid, :object_version, :name, :short_name, :organizational_unit, :operating_department_name, :code, :phone, :fax, :email, :registration_number, :url, :time_zone ) + fields = [:objectid, :object_version, :name, :short_name, :organizational_unit, :operating_department_name, :code, :phone, :fax, :email, :registration_number, :url, :time_zone] + fields += permitted_custom_fields_params(Chouette::Company.custom_fields(line_referential.workgroup)) + params.require(:company).permit( fields ) end private diff --git a/app/controllers/referential_companies_controller.rb b/app/controllers/referential_companies_controller.rb index 806a70c8f..200e56a89 100644 --- a/app/controllers/referential_companies_controller.rb +++ b/app/controllers/referential_companies_controller.rb @@ -40,11 +40,12 @@ class ReferentialCompaniesController < ChouetteController end @q = scope.search(params[:q]) - + ids = @q.result(:distinct => true).pluck(:id) + scope = scope.where(id: ids) if sort_column && sort_direction - @companies ||= @q.result(:distinct => true).order(sort_column + ' ' + sort_direction).paginate(:page => params[:page]) + @companies ||= scope.order(sort_column + ' ' + sort_direction).paginate(:page => params[:page]) else - @companies ||= @q.result(:distinct => true).order(:name).paginate(:page => params[:page]) + @companies ||= scope.order(:name).paginate(:page => params[:page]) end end @@ -57,7 +58,9 @@ class ReferentialCompaniesController < ChouetteController end def company_params - params.require(:company).permit( :objectid, :object_version, :name, :short_name, :organizational_unit, :operating_department_name, :code, :phone, :fax, :email, :registration_number, :url, :time_zone ) + fields = [:objectid, :object_version, :name, :short_name, :organizational_unit, :operating_department_name, :code, :phone, :fax, :email, :registration_number, :url, :time_zone] + fields += permitted_custom_fields_params(Chouette::Company.custom_fields(@referential.workgroup)) + params.require(:company).permit( fields ) end private diff --git a/app/models/chouette/company.rb b/app/models/chouette/company.rb index 53e412600..8d6dbee92 100644 --- a/app/models/chouette/company.rb +++ b/app/models/chouette/company.rb @@ -3,6 +3,8 @@ module Chouette include CompanyRestrictions include LineReferentialSupport include ObjectidSupport + include CustomFieldsSupport + has_paper_trail class_name: 'PublicVersion' has_many :lines diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb index 6b6558ca3..152f26256 100644 --- a/app/models/concerns/custom_fields_support.rb +++ b/app/models/concerns/custom_fields_support.rb @@ -5,12 +5,14 @@ module CustomFieldsSupport validate :custom_fields_values_are_valid after_initialize :initialize_custom_fields - def self.custom_fields - CustomField.where(resource_type: self.name.split("::").last) + def self.custom_fields workgroup=nil + fields = CustomField.where(resource_type: self.name.split("::").last) + fields = fields.where(workgroup_id: workgroup.id) if workgroup.present? + fields end - def custom_fields - CustomField::Collection.new self + def custom_fields workgroup=nil + CustomField::Collection.new self, workgroup end def custom_field_values= vals @@ -18,7 +20,7 @@ module CustomFieldsSupport custom_fields.each do |code, field| out[code] = field.preprocess_value_for_assignment(vals.symbolize_keys[code.to_sym]) end - self.write_attribute :custom_field_values, out + write_attribute :custom_field_values, out end def initialize_custom_fields diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 9fcfa9cd9..1db351135 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -5,13 +5,13 @@ class CustomField < ActiveRecord::Base enumerize :field_type, in: %i{list integer string attachment} validates :name, uniqueness: {scope: [:resource_type, :workgroup_id]} - validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false} + validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false}, presence: true scope :for_workgroup, ->(workgroup){ where workgroup_id: workgroup.id } class Collection < HashWithIndifferentAccess - def initialize object - vals = object.class.custom_fields.map do |v| + def initialize object, workgroup=nil + vals = object.class.custom_fields(workgroup).map do |v| [v.code, CustomField::Instance.new(object, v, object.custom_field_value(v.code))] end super Hash[*vals.flatten] @@ -166,14 +166,19 @@ class CustomField < ActiveRecord::Base end def display_value - options["list_values"][value.to_i] + return unless value + k = options["list_values"].is_a?(Hash) ? value.to_s : value.to_i + options["list_values"][k] end class Input < Base::Input def form_input_options + collection = options["list_values"] + collection = collection.map(&:reverse) if collection.is_a?(Hash) + collection = collection.each_with_index.to_a if collection.is_a?(Array) super.update({ selected: value, - collection: options["list_values"].map(&:reverse) + collection: collection }) end end diff --git a/app/views/companies/_form.html.slim b/app/views/companies/_form.html.slim index 3979c5800..e8b3fcede 100644 --- a/app/views/companies/_form.html.slim +++ b/app/views/companies/_form.html.slim @@ -12,7 +12,9 @@ = f.input :time_zone, include_blank: true = f.input :url = f.input :registration_number, :input_html => {:title => t("formtastic.titles#{format_restriction_for_locales(@line_referential)}.company.registration_number")} - + - if resource.custom_fields(current_referential.workgroup).any? + - resource.custom_fields.each do |code, field| + = field.input(f).to_s .separator = f.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'company_form' diff --git a/app/views/companies/show.html.slim b/app/views/companies/show.html.slim index ca0a410b3..8960b92dd 100644 --- a/app/views/companies/show.html.slim +++ b/app/views/companies/show.html.slim @@ -6,8 +6,11 @@ .container-fluid .row .col-lg-6.col-md-6.col-sm-12.col-xs-12 - = definition_list t('metadatas'), - { 'ID Codif' => @company.try(:get_objectid).try(:short_id), - Chouette::Company.human_attribute_name(:phone) => resource.phone, - Chouette::Company.human_attribute_name(:email) => resource.email, - Chouette::Company.human_attribute_name(:url) => resource.url } + - attributes = { t('id_codif') => @company.try(:objectid).try(:local_id), + Chouette::Company.human_attribute_name(:phone) => @company.phone, + Chouette::Company.human_attribute_name(:email) => @company.email, + Chouette::Company.human_attribute_name(:url) => @company.url } + - @company.custom_fields(current_referential.workgroup).each do |code, field| + - attributes.merge!(field.name => field.display_value) + + = definition_list t('metadatas'), attributes diff --git a/app/views/referential_companies/_form.html.slim b/app/views/referential_companies/_form.html.slim index b02eab3f1..0e7b20af4 100644 --- a/app/views/referential_companies/_form.html.slim +++ b/app/views/referential_companies/_form.html.slim @@ -1,18 +1,19 @@ -= semantic_form_for [@referential, @company] do |form| - = form.inputs do - = form.input :name, :input_html => { :title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.company.name") } - = form.input :short_name - = form.input :organizational_unit - = form.input :operating_department_name - = form.input :code - = form.input :phone, as: :phone - = form.input :fax, as: :phone - = form.input :email, as: :email - = form.input :time_zone, include_blank: true - = form.input :url - = form.input :registration_number, :input_html => { :title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.company.registration_number") } - = form.input :objectid, :required => !@company.new_record?, :input_html => { :title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.company.objectid") } - - = form.actions do - = form.action :submit, as: :button - = form.action :cancel, as: :link
\ No newline at end of file += simple_form_for [@referential, @company], html: {class: 'form-horizontal', id: 'company_form'}, wrapper: :horizontal_form do |f| + .row + .col-lg-12 + = f.input :name, :input_html => {:title => t("formtastic.titles#{format_restriction_for_locales(@line_referential)}.company.name")} + = f.input :short_name + = f.input :organizational_unit + = f.input :operating_department_name + = f.input :code + = f.input :phone + = f.input :fax + = f.input :email, as: :email + = f.input :time_zone, include_blank: true + = f.input :url + = f.input :registration_number, :input_html => {:title => t("formtastic.titles#{format_restriction_for_locales(@line_referential)}.company.registration_number")} + - if resource.custom_fields(@referential.workgroup).any? + - resource.custom_fields.each do |code, field| + = field.input(f).to_s + .separator + = f.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'company_form' diff --git a/app/views/referential_companies/edit.html.slim b/app/views/referential_companies/edit.html.slim index b3fcf6cd8..95be64aa1 100644 --- a/app/views/referential_companies/edit.html.slim +++ b/app/views/referential_companies/edit.html.slim @@ -1,3 +1,5 @@ - breadcrumb :referential_company, @referential, @company - page_header_content_for @company -= render 'form' +.page_content + .container-fluid + = render 'form' diff --git a/app/views/referential_companies/new.html.slim b/app/views/referential_companies/new.html.slim index 5e59db139..1dfdc8eb5 100644 --- a/app/views/referential_companies/new.html.slim +++ b/app/views/referential_companies/new.html.slim @@ -1,2 +1,6 @@ - breadcrumb :referential_companies, @referential -= render 'form' +.page_content + .container-fluid + .row + .col-lg-8.col-lg-offset-2.col-md-8.col-md-offset-2.col-sm-10.col-sm-offset-1 + = render 'form' diff --git a/app/views/referential_companies/show.html.slim b/app/views/referential_companies/show.html.slim index 1599145be..8ad011edf 100644 --- a/app/views/referential_companies/show.html.slim +++ b/app/views/referential_companies/show.html.slim @@ -17,8 +17,11 @@ .container-fluid .row .col-lg-6.col-md-6.col-sm-12.col-xs-12 - = definition_list t('metadatas'), - { t('id_codif') => @company.try(:objectid).try(:local_id), - Chouette::Company.human_attribute_name(:phone) => @company.phone, - Chouette::Company.human_attribute_name(:email) => @company.email, - Chouette::Company.human_attribute_name(:url) => @company.url } + - attributes = { t('id_codif') => @company.try(:objectid).try(:local_id), + Chouette::Company.human_attribute_name(:phone) => @company.phone, + Chouette::Company.human_attribute_name(:email) => @company.email, + Chouette::Company.human_attribute_name(:url) => @company.url } + - @company.custom_fields(@referential.workgroup).each do |code, field| + - attributes.merge!(field.name => field.display_value) + + = definition_list t('metadatas'), attributes diff --git a/db/migrate/20180316115003_add_custom_field_values_to_companies.rb b/db/migrate/20180316115003_add_custom_field_values_to_companies.rb new file mode 100644 index 000000000..1791a6970 --- /dev/null +++ b/db/migrate/20180316115003_add_custom_field_values_to_companies.rb @@ -0,0 +1,5 @@ +class AddCustomFieldValuesToCompanies < ActiveRecord::Migration + def change + add_column :companies, :custom_field_values, :json + end +end diff --git a/db/schema.rb b/db/schema.rb index 9cf5f8319..0f6f21b83 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -18,6 +18,7 @@ ActiveRecord::Schema.define(version: 20180319043333) do enable_extension "hstore" enable_extension "postgis" enable_extension "unaccent" + enable_extension "objectid" create_table "access_links", id: :bigserial, force: :cascade do |t| t.integer "access_point_id", limit: 8 @@ -140,6 +141,7 @@ ActiveRecord::Schema.define(version: 20180319043333) do t.text "import_xml" t.datetime "created_at" t.datetime "updated_at" + t.json "custom_field_values" end add_index "companies", ["line_referential_id"], name: "index_companies_on_line_referential_id", using: :btree |
