Class: RailsAdminImport::ImportModel
- Inherits:
-
Object
- Object
- RailsAdminImport::ImportModel
- Defined in:
- lib/rails_admin_import/import_model.rb
Instance Attribute Summary collapse
-
#abstract_model ⇒ Object
readonly
Returns the value of attribute abstract_model.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #associated_config(field) ⇒ Object
- #associated_model_fields(field) ⇒ Object
- #associated_object(field, mapping_field, value) ⇒ Object
- #association_class(field) ⇒ Object
- #association_fields ⇒ Object
- #belongs_to_fields ⇒ Object
- #display_name ⇒ Object
- #has_multiple_values?(field_name) ⇒ Boolean
- #importable_fields(model_config = config) ⇒ Object
-
#initialize(abstract_model) ⇒ ImportModel
constructor
A new instance of ImportModel.
- #label_for_model(object) ⇒ Object
- #label_method ⇒ Object
- #many_fields ⇒ Object
- #model_fields(model_config = config) ⇒ Object
- #pluralize_field(field_name) ⇒ Object
Constructor Details
#initialize(abstract_model) ⇒ ImportModel
Returns a new instance of ImportModel.
6 7 8 9 10 |
# File 'lib/rails_admin_import/import_model.rb', line 6 def initialize(abstract_model) @abstract_model = abstract_model @config = abstract_model.config.import @model = abstract_model.model end |
Instance Attribute Details
#abstract_model ⇒ Object (readonly)
Returns the value of attribute abstract_model.
12 13 14 |
# File 'lib/rails_admin_import/import_model.rb', line 12 def abstract_model @abstract_model end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/rails_admin_import/import_model.rb', line 12 def config @config end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
12 13 14 |
# File 'lib/rails_admin_import/import_model.rb', line 12 def model @model end |
Instance Method Details
#associated_config(field) ⇒ Object
69 70 71 |
# File 'lib/rails_admin_import/import_model.rb', line 69 def associated_config(field) field.associated_model_config.import end |
#associated_model_fields(field) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/rails_admin_import/import_model.rb', line 73 def associated_model_fields(field) @associated_fields ||= {} @associated_fields[field] ||= associated_config(field).visible_fields.select { |f| !f.association? } end |
#associated_object(field, mapping_field, value) ⇒ Object
59 60 61 62 63 |
# File 'lib/rails_admin_import/import_model.rb', line 59 def associated_object(field, mapping_field, value) klass = association_class(field) klass.where(mapping_field => value).first or raise AssociationNotFound, "#{klass}.#{mapping_field} = #{value}" end |
#association_class(field) ⇒ Object
65 66 67 |
# File 'lib/rails_admin_import/import_model.rb', line 65 def association_class(field) field.association.klass end |
#association_fields ⇒ Object
41 42 43 44 45 |
# File 'lib/rails_admin_import/import_model.rb', line 41 def association_fields @association_fields ||= importable_fields.select { |f| f.association? && !f.association.polymorphic? } end |
#belongs_to_fields ⇒ Object
47 48 49 50 51 |
# File 'lib/rails_admin_import/import_model.rb', line 47 def belongs_to_fields @belongs_to_fields ||= association_fields.select { |f| !f.multiple? } end |
#display_name ⇒ Object
14 15 16 |
# File 'lib/rails_admin_import/import_model.rb', line 14 def display_name abstract_model.config.label end |
#has_multiple_values?(field_name) ⇒ Boolean
80 81 82 83 |
# File 'lib/rails_admin_import/import_model.rb', line 80 def has_multiple_values?(field_name) plural_name = pluralize_field(field_name) many_fields.any? { |field| field.name == field_name || field.name == plural_name } end |
#importable_fields(model_config = config) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rails_admin_import/import_model.rb', line 26 def importable_fields(model_config = config) @importable_fields ||= {} @importable_fields[model_config] ||= model_config.visible_fields.reject do |f| # Exclude id, created_at and updated_at model_config.default_excluded_fields.include? f.name end end |
#label_for_model(object) ⇒ Object
18 19 20 |
# File 'lib/rails_admin_import/import_model.rb', line 18 def label_for_model(object) object.public_send(label_method) end |
#label_method ⇒ Object
22 23 24 |
# File 'lib/rails_admin_import/import_model.rb', line 22 def label_method @label_method ||= abstract_model.config.object_label_method end |
#many_fields ⇒ Object
53 54 55 56 57 |
# File 'lib/rails_admin_import/import_model.rb', line 53 def many_fields @many_fields ||= association_fields.select { |f| f.multiple? } end |
#model_fields(model_config = config) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/rails_admin_import/import_model.rb', line 34 def model_fields(model_config = config) @model_fields ||= {} @model_fields[model_config] ||= importable_fields(model_config).select { |f| !f.association? || f.association.polymorphic? } end |
#pluralize_field(field_name) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/rails_admin_import/import_model.rb', line 85 def pluralize_field(field_name) @plural_fields ||= many_fields.map(&:name).each_with_object({}) { |name, h| h[name.to_s.singularize.to_sym] = name } @plural_fields[field_name] || field_name end |