Class: ActiveRecord::Bixformer::Model::Base

Inherits:
Object
  • Object
show all
Includes:
ImportValueValidatable, ActiveRecord::Bixformer::ModelCallback
Defined in:
lib/activerecord-bixformer/model/base.rb

Direct Known Subclasses

Csv::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImportValueValidatable

#presence_value?

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, options)
  @name         = model_or_association_name.to_s
  @options      = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
  @associations = []
end

Instance Attribute Details

#associationsHash<String, ActiveRecord::Bixformer::Model::Base> (readonly)

the import/export target association names and its instance.

Returns:



15
16
17
# File 'lib/activerecord-bixformer/model/base.rb', line 15

def associations
  @associations
end

#attributesHash<String, ActiveRecord::Bixformer::Attribute::Base> (readonly)

the import/export target attribute names and its instance.

Returns:



15
16
17
# File 'lib/activerecord-bixformer/model/base.rb', line 15

def attributes
  @attributes
end

#nameString (readonly)

the name or association name of handled ActiveRecord

Returns:

  • (String)

    the current value of name



15
16
17
# File 'lib/activerecord-bixformer/model/base.rb', line 15

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/activerecord-bixformer/model/base.rb', line 19

def options
  @options
end

#parentActiveRecord::Bixformer::Model::Base (readonly)

the instance has parent association.

Returns:



15
16
17
# File 'lib/activerecord-bixformer/model/base.rb', line 15

def parent
  @parent
end

#preferred_skip_attributesArray<String> (readonly)

the list of attribute name to not make key if its value is blank.

Returns:

  • (Array<String>)

    the current value of preferred_skip_attributes



15
16
17
# File 'lib/activerecord-bixformer/model/base.rb', line 15

def preferred_skip_attributes
  @preferred_skip_attributes
end

#translatorActiveRecord::Bixformer::Translator::I18n (readonly)

Returns the current value of translator.

Returns:



15
16
17
# File 'lib/activerecord-bixformer/model/base.rb', line 15

def translator
  @translator
end

Instance Method Details

#activerecord_constantConstant

Returns the constant value of handling ActiveRecord.

Returns:

  • (Constant)

    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_keyString

Returns the foreign key name to associate to parent ActiveRecord.

Returns:

  • (String)

    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

#parentsObject



57
58
59
# File 'lib/activerecord-bixformer/model/base.rb', line 57

def parents
  @parent ? [*parent.parents, @parent] : []
end

#planObject



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, attribute_options = @plan.parse_to_type_and_options(attribute_value)

    @plan.new_module_instance(:attribute, attribute_type, self, attribute_name, attribute_options)
  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_includedObject



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