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
27
# 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 = []
  @errors       = ::ActiveRecord::Bixformer::Errors.new
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

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
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.



77
78
79
80
81
82
83
84
# File 'lib/activerecord-bixformer/model/base.rb', line 77

def activerecord_constant
  @activerecord_constant ||=
    if @parent
      @parent.activerecord_constant.reflections[@name].class_name.constantize
    else
      @name.camelize.constantize
    end
end

#add_association(model) ⇒ Object



70
71
72
73
74
# File 'lib/activerecord-bixformer/model/base.rb', line 70

def add_association(model)
  @associations.push(model)

  model.set_parent(self)
end

#find_record_by!(condition) ⇒ Object



138
139
140
# File 'lib/activerecord-bixformer/model/base.rb', line 138

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.



64
65
66
67
68
# File 'lib/activerecord-bixformer/model/base.rb', line 64

def parent_foreign_key
  return nil unless @parent

  @parent_foreign_key ||= @parent.activerecord_constant.reflections[@name].foreign_key
end

#parentsObject



59
60
61
# File 'lib/activerecord-bixformer/model/base.rb', line 59

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

#planObject



51
52
53
# File 'lib/activerecord-bixformer/model/base.rb', line 51

def plan
  @plan.raw_value
end

#set_parent(model) ⇒ Object



55
56
57
# File 'lib/activerecord-bixformer/model/base.rb', line 55

def set_parent(model)
  @parent = model
end

#setup(plan) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/activerecord-bixformer/model/base.rb', line 29

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, {})
  @sort_indexes              = @plan.pickup_value_for(self, :sort_indexes, {})

  # 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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/activerecord-bixformer/model/base.rb', line 86

def should_be_included
  arr  = []
  hash = {}

  symbolizer = -> (value) do
    case value
    when ::Hash
      value.map { |k, v| [k.to_sym, symbolizer.call(v)]}.to_h
    when ::Array
      value.map { |v| symbolizer.call(v) }
    when ::String
      value.to_sym
    else
      value
    end
  end

  @attributes.each do |attr|
    attr_should_be_included = attr.should_be_included

    next unless attr_should_be_included

    if attr_should_be_included.is_a?(::Hash)
      hash.merge!(symbolizer.call(attr_should_be_included))
    elsif attr_should_be_included.is_a?(::Array)
      arr.push *symbolizer.call(attr_should_be_included)
    else
      arr.push symbolizer.call(attr_should_be_included)
    end
  end

  @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



142
143
144
# File 'lib/activerecord-bixformer/model/base.rb', line 142

def translate(attribute_name)
  @translator.translate_attribute(attribute_name)
end