diff options
| -rw-r--r-- | app/controllers/stop_areas_controller.rb | 5 | ||||
| -rw-r--r-- | app/helpers/application_helper.rb | 6 | ||||
| -rw-r--r-- | app/models/chouette/stop_area.rb | 1 | ||||
| -rw-r--r-- | app/models/concerns/custom_fields_support.rb | 5 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 87 | ||||
| -rw-r--r-- | app/views/shared/custom_fields/_attachment.html.slim | 5 | ||||
| -rw-r--r-- | app/views/stop_areas/_form.html.slim | 7 | ||||
| -rw-r--r-- | app/views/stop_areas/show.html.slim | 2 | ||||
| -rw-r--r-- | config/initializers/simple_form_bootstrap.rb | 4 | ||||
| -rw-r--r-- | db/migrate/20180313082623_add_custom_field_values_to_stop_areas.rb | 5 | 
10 files changed, 112 insertions, 15 deletions
| diff --git a/app/controllers/stop_areas_controller.rb b/app/controllers/stop_areas_controller.rb index d0d9f652d..b2634467d 100644 --- a/app/controllers/stop_areas_controller.rb +++ b/app/controllers/stop_areas_controller.rb @@ -161,7 +161,7 @@ class StopAreasController < ChouetteController    helper_method :current_referential    def stop_area_params -    params.require(:stop_area).permit( +    fields = [        :area_type,        :children_ids,        :city_name, @@ -192,7 +192,8 @@ class StopAreasController < ChouetteController        :kind,        :status,        localized_names: Chouette::StopArea::AVAILABLE_LOCALIZATIONS -    ) +    ] + permitted_custom_fields_params(Chouette::StopArea.custom_fields) # XXX filter on the workgroup +    params.require(:stop_area).permit(fields)    end     # Fake ransack filter diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 479b661c8..a0c6796ea 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -136,5 +136,9 @@ module ApplicationHelper      url_for(:controller => "/help", :action => "show") + '/' + target    end - +  def permitted_custom_fields_params custom_fields +    [{ +      custom_field_values: custom_fields.map(&:code) +    }] +  end  end diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index e22c3996d..c263fa987 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -7,6 +7,7 @@ module Chouette      include StopAreaRestrictions      include StopAreaReferentialSupport      include ObjectidSupport +    include CustomFieldsSupport      extend Enumerize      enumerize :area_type, in: Chouette::AreaType::ALL diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb index 7d2c22537..6565ac808 100644 --- a/app/models/concerns/custom_fields_support.rb +++ b/app/models/concerns/custom_fields_support.rb @@ -15,13 +15,14 @@ module CustomFieldsSupport      def custom_field_values= vals        out = {} -      vals.each do |k, val| -        out[k] = custom_fields[k]&.preprocess_value_for_assignment(val) +      custom_fields.each do |code, field| +        out[code] = field.preprocess_value_for_assignment(vals[code])        end        self.write_attribute :custom_field_values, out      end      def initialize_custom_fields +      self.custom_field_values ||= {}        custom_fields.values.each &:initialize_custom_field      end diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 98cb38614..a42f1651d 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -7,10 +7,12 @@ class CustomField < ActiveRecord::Base    validates :name, uniqueness: {scope: [:resource_type, :workgroup_id]}    validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false} +  scope :for_workgroup, ->(workgroup){ where workgroup_id: workgroup.id } +    class Collection < HashWithIndifferentAccess      def initialize object        vals = object.class.custom_fields.map do |v| -        [v.code, CustomField::Value.new(object, v, object.custom_field_value(v.code))] +        [v.code, CustomField::Instance.new(object, v, object.custom_field_value(v.code))]        end        super Hash[*vals.flatten]      end @@ -20,11 +22,11 @@ class CustomField < ActiveRecord::Base      end    end -  class Value +  class Instance      def self.new owner, custom_field, value        field_type = custom_field.field_type -      klass_name = field_type && "CustomField::Value::#{field_type.classify}" -      klass = klass_name && const_defined?(klass_name) ? klass_name.constantize : CustomField::Value::Base +      klass_name = field_type && "CustomField::Instance::#{field_type.classify}" +      klass = klass_name.safe_constantize || CustomField::Instance::Base        klass.new owner, custom_field, value      end @@ -37,7 +39,8 @@ class CustomField < ActiveRecord::Base          @validated = false          @valid = false        end -      attr_accessor :owner + +      attr_accessor :owner, :custom_field        delegate :code, :name, :field_type, to: :@custom_field @@ -58,6 +61,14 @@ class CustomField < ActiveRecord::Base          @raw_value        end +      def input form_helper +        @input ||= begin +          klass_name = field_type && "CustomField::Instance::#{field_type.classify}::Input" +          klass = klass_name.safe_constantize || CustomField::Instance::Base::Input +          klass.new self, form_helper +        end +      end +        def errors_key          "custom_fields.#{code}"        end @@ -83,6 +94,46 @@ class CustomField < ActiveRecord::Base            :locals => { field: self}          )        end + +      class Input +        def initialize instance, form_helper +          @instance = instance +          @form_helper = form_helper +        end + +        def custom_field +          @instance.custom_field +        end + +        delegate :custom_field, :value, :options, to: :@instance +        delegate :code, :name, :field_type, to: :custom_field + +        def to_s +          out = form_input +          out.html_safe +        end + +        protected + +        def form_input_id +          "custom_field_#{code}" +        end + +        def form_input_name +          "#{@form_helper.object_name}[custom_field_values][#{code}]" +        end + +        def form_input_options +          { +            input_html: {value: value, name: form_input_name}, +            label: name +          } +        end + +        def form_input +          @form_helper.input form_input_id, form_input_options +        end +      end      end      class Integer < Base @@ -111,7 +162,16 @@ class CustomField < ActiveRecord::Base        end        def display_value -        options["list_values"][value] +        options["list_values"][value.to_s] +      end + +      class Input < Base::Input +        def form_input_options +          super.update({ +            selected: value, +            collection: options["list_values"].map(&:reverse) +          }) +        end        end      end @@ -152,7 +212,11 @@ class CustomField < ActiveRecord::Base        end        def preprocess_value_for_assignment val -        owner.send "#{uploader_name}=", val +        if val.present? +          owner.send "#{uploader_name}=", val +        else +          @raw_value +        end        end        def value @@ -174,6 +238,15 @@ class CustomField < ActiveRecord::Base        def display_value          render_partial        end + +      class Input < Base::Input +        def form_input_options +          super.update({ +            as: :file, +            wrapper: :horizontal_file_input +          }) +        end +      end      end      class String < Base diff --git a/app/views/shared/custom_fields/_attachment.html.slim b/app/views/shared/custom_fields/_attachment.html.slim index 2f25b396a..32d0fda4d 100644 --- a/app/views/shared/custom_fields/_attachment.html.slim +++ b/app/views/shared/custom_fields/_attachment.html.slim @@ -1 +1,4 @@ -= link_to I18n.t("custom_fields.#{field.owner.class.name.demodulize.underscore}.#{field.code}.link"), field.value.url +- if field.value.present? +  = link_to I18n.t("custom_fields.#{field.owner.class.name.demodulize.underscore}.#{field.code}.link"), field.value.url +- else +  = "-" diff --git a/app/views/stop_areas/_form.html.slim b/app/views/stop_areas/_form.html.slim index 20bdc289f..90dd187ee 100644 --- a/app/views/stop_areas/_form.html.slim +++ b/app/views/stop_areas/_form.html.slim @@ -62,6 +62,13 @@              = f.input :stairs_availability, as: :select, :collection => [[t("true"), true], [t("false"), false]], :include_blank => true              = f.input :lift_availability, as: :select, :collection => [[t("true"), true], [t("false"), false]], :include_blank => true +        // XXX filter on the workgroup +        - if resource.custom_fields.any? +          .custom_fields +            h3 = t("stop_areas.stop_area.custom_fields") +            - 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: 'stop_area_form' diff --git a/app/views/stop_areas/show.html.slim b/app/views/stop_areas/show.html.slim index a6147b86d..851bd9b82 100644 --- a/app/views/stop_areas/show.html.slim +++ b/app/views/stop_areas/show.html.slim @@ -23,4 +23,6 @@              t('activerecord.attributes.stop_area.state') => stop_area_status(@stop_area),              @stop_area.human_attribute_name(:comment) => @stop_area.try(:comment),              }) +        - @stop_area.custom_fields.each do |code, field| +          - attributes.merge!(field.name => field.display_value)          = definition_list t('metadatas'), attributes diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb index 4b9bd320d..8dbc3afee 100644 --- a/config/initializers/simple_form_bootstrap.rb +++ b/config/initializers/simple_form_bootstrap.rb @@ -72,9 +72,9 @@ SimpleForm.setup do |config|      b.use :placeholder      b.optional :maxlength      b.optional :readonly -    b.use :label, class: 'col-sm-3 control-label' +    b.use :label, class: 'col-sm-4 col-xs-5 control-label' -    b.wrapper tag: 'div', class: 'col-sm-9' do |ba| +    b.wrapper tag: 'div', class: 'col-sm-8 col-xs-7' do |ba|        ba.use :input        ba.use :error, wrap_with: { tag: 'span', class: 'help-block small' }        ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block small' } diff --git a/db/migrate/20180313082623_add_custom_field_values_to_stop_areas.rb b/db/migrate/20180313082623_add_custom_field_values_to_stop_areas.rb new file mode 100644 index 000000000..1b901d139 --- /dev/null +++ b/db/migrate/20180313082623_add_custom_field_values_to_stop_areas.rb @@ -0,0 +1,5 @@ +class AddCustomFieldValuesToStopAreas < ActiveRecord::Migration +  def change +    add_column :stop_areas, :custom_field_values, :json +  end +end | 
