Class: Decidim::Maintenance::TaxonomyPlan
- Inherits:
-
Object
- Object
- Decidim::Maintenance::TaxonomyPlan
- Defined in:
- decidim-core/lib/decidim/maintenance/taxonomy_plan.rb
Instance Attribute Summary collapse
-
#importer ⇒ Object
readonly
Returns the value of attribute importer.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#organization ⇒ Object
readonly
Returns the value of attribute organization.
Instance Method Summary collapse
- #existing_taxonomies ⇒ Object
- #import(data) ⇒ Object
- #imported_taxonomies ⇒ Object
-
#initialize(organization, models, importer: TaxonomyImporter) ⇒ TaxonomyPlan
constructor
A new instance of TaxonomyPlan.
- #to_h ⇒ Object
- #to_json(*_args) ⇒ Object
Constructor Details
#initialize(organization, models, importer: TaxonomyImporter) ⇒ TaxonomyPlan
Returns a new instance of TaxonomyPlan.
6 7 8 9 10 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 6 def initialize(organization, models, importer: TaxonomyImporter) @organization = organization @models = models @importer = importer end |
Instance Attribute Details
#importer ⇒ Object (readonly)
Returns the value of attribute importer.
12 13 14 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 12 def importer @importer end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
12 13 14 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 12 def models @models end |
#organization ⇒ Object (readonly)
Returns the value of attribute organization.
12 13 14 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 12 def organization @organization end |
Instance Method Details
#existing_taxonomies ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 55 def existing_taxonomies organization.taxonomies.roots.to_h do |root| [ translate(root.name), root.children.to_h do |taxonomy| [ translate(taxonomy.name), taxonomy.children.map do |child| translate(child.name) end ] end ] end end |
#import(data) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 14 def import(data, &) data["imported_taxonomies"].each do |model_name, taxonomies| klass = importer.new(organization, taxonomies) if block_given? yield klass, model_name else klass.import! end end end |
#imported_taxonomies ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 44 def imported_taxonomies return @imported_taxonomies if defined?(@imported_taxonomies) @imported_taxonomies = {} models.map do |model| yield model if block_given? @imported_taxonomies[model.table_name] = model.with(organization).to_taxonomies end @imported_taxonomies end |
#to_h ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 31 def to_h(&) { organization: { id: organization.id, locale: organization.default_locale, host: organization.host, name: translate(organization.name) }, existing_taxonomies:, imported_taxonomies: imported_taxonomies(&) } end |
#to_json(*_args) ⇒ Object
25 26 27 28 29 |
# File 'decidim-core/lib/decidim/maintenance/taxonomy_plan.rb', line 25 def to_json(*_args, &) I18n.with_locale(organization.default_locale) do JSON.pretty_generate(to_h(&)) end end |