Class: Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/watcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models) ⇒ Watcher

Returns a new instance of Watcher.



3
4
5
6
# File 'lib/watcher.rb', line 3

def initialize(models)
  self.models = models
  @association_tables = {}
end

Instance Attribute Details

#modelsObject

Returns the value of attribute models.



2
3
4
# File 'lib/watcher.rb', line 2

def models
  @models
end

Instance Method Details

#foreign_keys_for(model) ⇒ Object



8
9
10
11
12
13
# File 'lib/watcher.rb', line 8

def foreign_keys_for(model)
  model
    .reflections
    .select {|reflection_name, reflection| reflection.macro == :belongs_to && !reflection.options[:polymorphic]}
    .map { |reflection_name, reflection| reflection.foreign_key }
end

#plurality_for(reflection) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/watcher.rb', line 124

def plurality_for(reflection)
  case reflection.macro
  when :belongs_to, :has_one
    false
  else
    true
  end
end

#seriaized_association(reflection_name, reflection) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/watcher.rb', line 23

def seriaized_association(reflection_name, reflection)
  foreign_key_on_foreign_table =
    case
    when reflection.options[:through]
      reflection.source_reflection.macro.in? i{has_many has_one}
    else
      reflection.macro.in? i{has_many has_one}
    end
  return [] if reflection.options[:as]
  ret = {
    name: reflection_name,
    joins: reflection.class_name,
    foreign_table: reflection.table_name,
    foreign_key: foreign_key_on_foreign_table ? reflection.foreign_key : reflection.association_primary_key,
    primary_key: foreign_key_on_foreign_table ? reflection.association_primary_key : reflection.foreign_key,
    type: reflection.macro,
    through: reflection.options[:through],
    inverse_of: reflection.inverse_of.try(:name),
  }

  sec = nil

  if reflection.macro == :has_and_belongs_to_many
    ret[:through] = reflection.join_table
    ret[:type] = :has_many
    ret[:primary_key] = reflection.association_foreign_key
    sec = {
      name: reflection.join_table,
      joins: reflection.join_table.camelize,
      foreign_table: reflection.join_table,
      foreign_key: reflection.foreign_key,
      primary_key: reflection.association_primary_key,
      type: :has_many
    }
  end

  [ret.compact, sec].compact
end

#serializable_hashObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/watcher.rb', line 100

def serializable_hash
  models.map do |model|
    foreign_keys =  foreign_keys_for(model)
    # find association tables
    model
      .reflections
      .select { |_, reflection| reflection.class_name.in?(models.map(&:to_s)) && reflection.macro == :has_and_belongs_to_many }
      .each { |reflection_name, reflection| serialize_habtam_reflection(model, reflection) }
    {
      name: model.to_s,
      table_name: model.table_name,
      columns:
        model
          .column_types
          .map { |column_name, column_type| serialized_column(column_name, column_type, model, foreign_keys) },
      associations:
        model
          .reflections
          .select { |_, reflection| reflection.class_name.in?(models.map(&:to_s)) }
          .flat_map { |reflection_name, reflection| seriaized_association(reflection_name, reflection) }
    }
  end + @association_tables.values
end

#serialize_habtam_reflection(model, reflection) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/watcher.rb', line 62

def serialize_habtam_reflection(model, reflection)
  name = reflection.join_table.camelize
  @association_tables[name] = {
    name: name,
    table_name: reflection.join_table,
    columns: [
      {
        name: reflection.association_foreign_key,
        type: :integer,
        foreign_key: true
      },
      {
        name: reflection.foreign_key,
        type: :integer,
        foreign_key: true
      }
    ],
    associations: [
      {
        name: reflection.class_name.underscore,
        joins: reflection.class_name,
        foreign_table: reflection.table_name,
        foreign_key: reflection.association_primary_key,
        primary_key: reflection.association_foreign_key,
        type: :belongs_to
      },
      {
        name: model.to_s.underscore,
        joins: model.to_s,
        foreign_table: model.table_name,
        foreign_key: reflection.association_primary_key,
        primary_key: reflection.foreign_key,
        type: :belongs_to
      }
    ]
  }
end

#serialized_column(column_name, column_type, model, foreign_keys) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/watcher.rb', line 15

def serialized_column(column_name, column_type, model, foreign_keys)
  ret = { name: column_name, type: column_type.type }

  ret[:primary_key] = true if column_name == model.primary_key
  ret[:foreign_key] = true if foreign_keys.include?(column_name)
  ret
end