Class: FuryDumper::Dumpers::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/fury_dumper/dumpers/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_model:, relation_items:, iteration: 0, root_model: nil) ⇒ Model

Returns a new instance of Model.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fury_dumper/dumpers/model.rb', line 9

def initialize(source_model:, relation_items:, iteration: 0, root_model: nil)
  raise ArgumentError unless source_model.is_a?(String)
  raise ArgumentError unless relation_items.is_a?(RelationItems)
  raise ArgumentError unless iteration.is_a?(Numeric)
  raise ArgumentError unless root_model.is_a?(Model) || root_model.nil?

  @source_model   = source_model
  @relation_items = relation_items
  @iteration      = iteration
  @root_model     = root_model
  @warnings       = []
end

Instance Attribute Details

#iterationObject (readonly)

Returns the value of attribute iteration.



6
7
8
# File 'lib/fury_dumper/dumpers/model.rb', line 6

def iteration
  @iteration
end

#relation_itemsObject

Returns the value of attribute relation_items.



7
8
9
# File 'lib/fury_dumper/dumpers/model.rb', line 7

def relation_items
  @relation_items
end

#root_modelObject

Returns the value of attribute root_model.



7
8
9
# File 'lib/fury_dumper/dumpers/model.rb', line 7

def root_model
  @root_model
end

#source_modelObject (readonly)

Returns the value of attribute source_model.



6
7
8
# File 'lib/fury_dumper/dumpers/model.rb', line 6

def source_model
  @source_model
end

#warningsObject (readonly)

Returns the value of attribute warnings.



6
7
8
# File 'lib/fury_dumper/dumpers/model.rb', line 6

def warnings
  @warnings
end

Instance Method Details

#==(other) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
# File 'lib/fury_dumper/dumpers/model.rb', line 35

def ==(other)
  raise ArgumentError unless other.is_a?(Model)

  @source_model == other.source_model &&
    @relation_items.eql?(other.relation_items) &&
    sub_path?(other)
end

#active_record_modelObject



72
73
74
# File 'lib/fury_dumper/dumpers/model.rb', line 72

def active_record_model
  @source_model.constantize
end

#all_non_scoped_modelsObject



100
101
102
103
104
105
106
107
# File 'lib/fury_dumper/dumpers/model.rb', line 100

def all_non_scoped_models
  active_record_model.reflect_on_all_associations.select { |rr| rr.scope.nil? }.map do |rr|
    rr.klass.to_s
  rescue NameError, LoadError => e
    @warnings << e
    next
  end.compact.uniq
end

#column_namesObject



31
32
33
# File 'lib/fury_dumper/dumpers/model.rb', line 31

def column_names
  active_record_model.columns.map(&:name)
end

#copyObject



22
23
24
25
26
27
# File 'lib/fury_dumper/dumpers/model.rb', line 22

def copy
  self.class.new(source_model: source_model,
                 relation_items: relation_items.copy,
                 iteration: iteration,
                 root_model: root_model)
end

#fetch_complex_itemsObject



82
83
84
# File 'lib/fury_dumper/dumpers/model.rb', line 82

def fetch_complex_items
  @relation_items.complex_items.map(&:key).join(' AND ')
end

#fetch_equality_items_hashObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fury_dumper/dumpers/model.rb', line 86

def fetch_equality_items_hash
  values = {}
  @relation_items.equality_items.each do |item|
    if active_record_model.column_names.include?(item.key)
      values[item.key] = item.values_for_key
    else
      @warnings << "Shit relation: #{@source_model}.#{item.key} does not exist"
      next
    end
  end

  values
end

#next_iterationObject



126
127
128
# File 'lib/fury_dumper/dumpers/model.rb', line 126

def next_iteration
  @iteration + 1
end

#root_pathObject



50
51
52
53
54
# File 'lib/fury_dumper/dumpers/model.rb', line 50

def root_path
  return [nil] if @root_model.nil?

  @root_model.root_path + [@root_model.source_model]
end

#sub_path?(other_model) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


43
44
45
46
47
48
# File 'lib/fury_dumper/dumpers/model.rb', line 43

def sub_path?(other_model)
  raise ArgumentError unless other_model.is_a?(Model)

  min_length = [root_path.length, other_model.root_path.length].min - 1
  root_path[0..min_length] == other_model.root_path[0..min_length]
end

#to_active_record_relationObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fury_dumper/dumpers/model.rb', line 109

def to_active_record_relation
  return @active_record_relation if @active_record_relation

  complex_values          = fetch_complex_items
  equality_values         = fetch_equality_items_hash
  @active_record_relation = active_record_model.where(equality_values)
                                               .where(complex_values)
                                               .limit(FuryDumper::Config.limit)

  unless FuryDumper::Config.fast?
    order_value = { primary_key => :desc }
    @active_record_relation = @active_record_relation.order(order_value)
  end

  @active_record_relation
end

#to_full_strObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fury_dumper/dumpers/model.rb', line 56

def to_full_str
  buffer = "MODEL #{@source_model} by #{root_model&.source_model.presence || '-'} WHERE "

  buffer += @relation_items.items.map do |item|
    if item.complex
      item.key
    elsif item.values_for_key.count > 10
      "#{item.key} = #{item.values_for_key[0..10]} and #{item.values_for_key.count - 10} elements"
    else
      "#{item.key} = #{item.values_for_key}"
    end
  end.join(' AND ')

  buffer
end

#to_short_strObject



78
79
80
# File 'lib/fury_dumper/dumpers/model.rb', line 78

def to_short_str
  "#{@source_model}.#{@relation_items.keys.join(' & ')}"
end