Class: Decidim::Admin::TaxonomyItemForm

Inherits:
Form show all
Includes:
TranslatableAttributes
Defined in:
decidim-admin/app/forms/decidim/admin/taxonomy_item_form.rb

Overview

A form object to be used when creating or updating a taxonomy.

Constant Summary

Constants included from Decidim::AttributeObject::TypeMap

Decidim::AttributeObject::TypeMap::Boolean, Decidim::AttributeObject::TypeMap::Decimal

Instance Attribute Summary

Attributes inherited from Decidim::AttributeObject::Form

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TranslatableAttributes

#attachment?, #default_locale?

Methods inherited from Decidim::AttributeObject::Form

ensure_hash, from_model, hash_from, infer_model_name, mimic, mimicked_model_name, model_name, #persisted?, #to_key, #to_model, #to_param, #valid?, #with_context

Methods included from Decidim::AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Class Method Details

.from_params(params, additional_params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'decidim-admin/app/forms/decidim/admin/taxonomy_item_form.rb', line 25

def self.from_params(params, additional_params = {})
  additional_params[:taxonomy] = {}
  if params[:taxonomy]
    params[:taxonomy].each do |key, value|
      additional_params[:taxonomy][key[8..]] = value if key.start_with?("item_name_")
    end
  end
  super
end

Instance Method Details

#map_model(model) ⇒ Object



21
22
23
# File 'decidim-admin/app/forms/decidim/admin/taxonomy_item_form.rb', line 21

def map_model(model)
  self.item_name = model.name
end

#parentObject



49
50
51
# File 'decidim-admin/app/forms/decidim/admin/taxonomy_item_form.rb', line 49

def parent
  @parent ||= Decidim::Taxonomy.find_by(id: parent_id)
end

#validate_parent_id_within_same_root_taxonomyObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'decidim-admin/app/forms/decidim/admin/taxonomy_item_form.rb', line 35

def validate_parent_id_within_same_root_taxonomy
  if parent
    current_root_taxonomy = if parent.root?
                              parent
                            else
                              parent.root_taxonomy
                            end

    errors.add(:parent_id, :invalid) unless parent.root_taxonomy.id == current_root_taxonomy.id
  else
    errors.add(:parent_id, :invalid)
  end
end