Class: ActiveRecord::Bixformer::Model::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Bixformer::Model::Base
- Defined in:
- lib/activerecord-bixformer/model/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#associations ⇒ Hash<String, ActiveRecord::Bixformer::Model::Base>
readonly
the import/export target association names and its instance.
-
#attributes ⇒ Hash<String, ActiveRecord::Bixformer::Attribute::Base>
readonly
the import/export target attribute names and its instance.
-
#name ⇒ String
readonly
the name or association name of handled ActiveRecord.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ ActiveRecord::Bixformer::Model::Base
readonly
the instance has parent association.
-
#preferred_skip_attributes ⇒ Array<String>
readonly
the list of attribute name to not make key if its value is blank.
-
#translator ⇒ ActiveRecord::Bixformer::Translator::I18n
readonly
The current value of translator.
Instance Method Summary collapse
-
#activerecord_constant ⇒ Constant
The constant value of handling ActiveRecord.
- #add_association(model) ⇒ Object
- #find_record_by!(condition) ⇒ Object
-
#initialize(model_or_association_name, options) ⇒ Base
constructor
A new instance of Base.
-
#parent_foreign_key ⇒ String
The foreign key name to associate to parent ActiveRecord.
- #parents ⇒ Object
- #plan ⇒ Object
- #set_parent(model) ⇒ Object
- #setup(plan) ⇒ Object
- #should_be_included ⇒ Object
- #translate(attribute_name) ⇒ Object
Methods included from ImportValueValidatable
Constructor Details
#initialize(model_or_association_name, options) ⇒ Base
Returns a new instance of Base.
22 23 24 25 26 |
# File 'lib/activerecord-bixformer/model/base.rb', line 22 def initialize(model_or_association_name, ) @name = model_or_association_name.to_s @options = (.is_a?(::Hash) ? : {}).with_indifferent_access @associations = [] end |
Instance Attribute Details
#associations ⇒ Hash<String, ActiveRecord::Bixformer::Model::Base> (readonly)
the import/export target association names and its instance.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def associations @associations end |
#attributes ⇒ Hash<String, ActiveRecord::Bixformer::Attribute::Base> (readonly)
the import/export target attribute names and its instance.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def attributes @attributes end |
#name ⇒ String (readonly)
the name or association name of handled ActiveRecord
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/activerecord-bixformer/model/base.rb', line 19 def @options end |
#parent ⇒ ActiveRecord::Bixformer::Model::Base (readonly)
the instance has parent association.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def parent @parent end |
#preferred_skip_attributes ⇒ Array<String> (readonly)
the list of attribute name to not make key if its value is blank.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def preferred_skip_attributes @preferred_skip_attributes end |
#translator ⇒ ActiveRecord::Bixformer::Translator::I18n (readonly)
Returns the current value of translator.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def translator @translator end |
Instance Method Details
#activerecord_constant ⇒ Constant
Returns the constant value of handling ActiveRecord.
75 76 77 78 79 80 81 82 |
# File 'lib/activerecord-bixformer/model/base.rb', line 75 def activerecord_constant @activerecord_constant ||= if @parent @parent.activerecord_constant.reflections[@name].table_name.classify.constantize else @name.camelize.constantize end end |
#add_association(model) ⇒ Object
68 69 70 71 72 |
# File 'lib/activerecord-bixformer/model/base.rb', line 68 def add_association(model) @associations.push(model) model.set_parent(self) end |
#find_record_by!(condition) ⇒ Object
109 110 111 |
# File 'lib/activerecord-bixformer/model/base.rb', line 109 def find_record_by!(condition) activerecord_constant.find_by!(condition) end |
#parent_foreign_key ⇒ String
Returns the foreign key name to associate to parent ActiveRecord.
62 63 64 65 66 |
# File 'lib/activerecord-bixformer/model/base.rb', line 62 def parent_foreign_key return nil unless @parent @parent_foreign_key ||= @parent.activerecord_constant.reflections[@name].foreign_key end |
#parents ⇒ Object
57 58 59 |
# File 'lib/activerecord-bixformer/model/base.rb', line 57 def parents @parent ? [*parent.parents, @parent] : [] end |
#plan ⇒ Object
49 50 51 |
# File 'lib/activerecord-bixformer/model/base.rb', line 49 def plan @plan.raw_value end |
#set_parent(model) ⇒ Object
53 54 55 |
# File 'lib/activerecord-bixformer/model/base.rb', line 53 def set_parent(model) @parent = model end |
#setup(plan) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/activerecord-bixformer/model/base.rb', line 28 def setup(plan) @plan = ActiveRecord::Bixformer::PlanAccessor.new(plan) entry = @plan.pickup_value_for(self, :entry, {}) @attributes = (entry[:attributes] || {}).map do |attribute_name, attribute_value| attribute_type, = @plan.(attribute_value) @plan.new_module_instance(:attribute, attribute_type, self, attribute_name, ) end @preferred_skip_attributes = @plan.pickup_value_for(self, :preferred_skip_attributes, []) @default_values = @plan.pickup_value_for(self, :default_values, {}) # At present, translation function is only i18n @translator = ::ActiveRecord::Bixformer::Translator::I18n.new @translator.config = @plan.value_of(:translation_config).dup @translator.model = self end |
#should_be_included ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/activerecord-bixformer/model/base.rb', line 84 def should_be_included arr = [] hash = {} @associations.each do |assoc| assoc_should_be_included = assoc.should_be_included if assoc_should_be_included.empty? arr.push assoc.name.to_sym else hash[assoc.name.to_sym] = assoc_should_be_included end end if hash.empty? && arr.empty? [] elsif hash.empty? arr elsif arr.empty? hash else [*arr, hash] end end |
#translate(attribute_name) ⇒ Object
113 114 115 |
# File 'lib/activerecord-bixformer/model/base.rb', line 113 def translate(attribute_name) @translator.translate_attribute(attribute_name) end |