Sha256: 4331799aeade7b1e0d5fd09862447fc59b09111e5874d36fd19f513cd722e98b

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      class TranslationSetSubjectForm < Decidim::Form
        attribute :subject_manifest, String
        attribute :subject_id, Integer
        attribute :component_model, Array[TermCustomizer::Admin::TranslationSetSubjectComponentForm]

        def map_model(model)
          component = model if model.is_a?(Decidim::Component)
          subject = if component
                      model.participatory_space
                    else
                      model
                    end

          self.subject_manifest = Decidim.participatory_space_manifests.find do |m|
            m.model_class_name == subject.class.name
          end.try(:name)
          self.subject_id = subject.id

          return unless component

          self.component_model = [
            TermCustomizer::Admin::TranslationSetSubjectComponentForm.from_params(
              subject_id: subject.id,
              component_id: component.id
            )
          ]
        end

        def subject
          return unless manifest

          @subject ||= manifest.model_class_name.constantize.find_by(id: subject_id)
        end

        def component
          return unless component_form
          return unless subject

          subject.components.find_by(id: component_form.component_id)
        end

        def manifest
          @manifest ||= Decidim.participatory_space_manifests.find do |m|
            m.name == subject_manifest.to_sym
          end
        end

        def component_form
          @component_form ||= component_model.find do |cm|
            cm.subject_id == subject_id
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-term_customizer-0.16.2 app/forms/decidim/term_customizer/admin/translation_set_subject_form.rb
decidim-term_customizer-0.16.1 app/forms/decidim/term_customizer/admin/translation_set_subject_form.rb
decidim-term_customizer-0.16.0 app/forms/decidim/term_customizer/admin/translation_set_subject_form.rb