Class: Whiteprint::Adapters::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/whiteprint/adapters/active_record.rb

Direct Known Subclasses

HasAndBelongsToMany

Defined Under Namespace

Classes: HasAndBelongsToMany, Migration

Constant Summary collapse

BELONGS_TO_OPTIONS =
[:class_name, :anonymous_class, :foreign_key, :validate, :autosave,
:dependent, :primary_key, :inverse_of, :required, :foreign_type,
:polymorphic, :touch, :counter_cache, :cached]

Instance Attribute Summary

Attributes inherited from Base

#attributes, #configs, #model, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#changes?, #clone_to, #execute, #explanation, load_plugins, #set_model, #table_name, #transformer

Constructor Details

#initialize(model, id: true, timestamps: true, auto_belongs_to: true, **_options) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



58
59
60
61
62
63
64
65
66
67
# File 'lib/whiteprint/adapters/active_record.rb', line 58

def initialize(model, id: true, timestamps: true, auto_belongs_to: true, **_options)
  super(model, id: true, timestamps: true, **_options)

  @has_id          = id
  @has_timestamps  = timestamps
  @auto_belongs_to = auto_belongs_to
  @attributes.add(name: :id, type: :integer, null: false) if id
  @attributes.add(name: :created_at, type: :datetime)     if timestamps
  @attributes.add(name: :updated_at, type: :datetime)     if timestamps
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(type, name, **options) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/whiteprint/adapters/active_record.rb', line 179

def method_missing(type, name, **options)
  super

  if options[:default] && options[:default].is_a?(Symbol)
    model.send :after_initialize do
      next if self.send(name) || !new_record?
      self.send "#{name}=", send(options[:default])
    end
  end
end

Class Method Details

.applicable?(model) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/whiteprint/adapters/active_record.rb', line 12

def applicable?(model)
  return false unless defined?(::ActiveRecord)
  model < ::ActiveRecord::Base
end

.camelize(name) ⇒ Object



22
23
24
25
26
# File 'lib/whiteprint/adapters/active_record.rb', line 22

def camelize(name)
  name = underscore(name)
  name = name.gsub(/^([a-z])/) { Regexp.last_match[1].upcase }
  name.gsub(/_([a-zA-Z])/) { Regexp.last_match[1].upcase }
end

.generate_migration(name, trees) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/whiteprint/adapters/active_record.rb', line 28

def generate_migration(name, trees)
  filename = "#{Time.now.strftime('%Y%m%d%H%M%S')}_#{underscore(name)}.rb"
  path     = File.join(Whiteprint.config.migration_path, filename)
  File.open(path, 'w') do |f|
    f.write migration(name, trees)
  end
  path
end

.migrateObject



37
38
39
40
# File 'lib/whiteprint/adapters/active_record.rb', line 37

def migrate
  ::ActiveRecord::Migration.verbose = true
  ::ActiveRecord::Migrator.migrate(::ActiveRecord::Migrator.migrations_paths)
end

.migration(name, trees) ⇒ Object



42
43
44
# File 'lib/whiteprint/adapters/active_record.rb', line 42

def migration(name, trees)
  "class #{camelize(name)} < ActiveRecord::Migration\n  def change\n" + transform(trees) + "  end\nend\n"
end

.migration_params(cli) ⇒ Object



46
47
48
49
# File 'lib/whiteprint/adapters/active_record.rb', line 46

def migration_params(cli)
  name = cli.ask 'How would you like to name this migration?'
  [name]
end

.underscore(name) ⇒ Object



17
18
19
20
# File 'lib/whiteprint/adapters/active_record.rb', line 17

def underscore(name)
  name = name.tr(' ', '_')
  name.gsub(/([a-z])([A-Z])/) { "#{Regexp.last_match[1]}_#{Regexp.last_match[2].downcase}" }.downcase
end

Instance Method Details

#accessor(name, options) ⇒ Object



69
70
71
72
# File 'lib/whiteprint/adapters/active_record.rb', line 69

def accessor(name, options)
  @attributes.add(name: name, type: :accessor, virtual: true, **options)
  model.send :attr_accessor, name
end

#changes_treeObject



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/whiteprint/adapters/active_record.rb', line 74

def changes_tree
  changes_tree = super

  unless changes_tree[:table_exists]
    changes_tree[:has_id] = @has_id
    changes_tree[:attributes].reject! { |attribute| attribute[:name] == :id }
  end

  added_created_at = changes_tree[:attributes].select { |attribute| attribute[:name] == :created_at && attribute[:kind] == :added }
  added_updated_at = changes_tree[:attributes].select { |attribute| attribute[:name] == :updated_at && attribute[:kind] == :added }

  if added_created_at.size == 1 && added_updated_at.size == 1
    changes_tree[:attributes] -= [*added_created_at, *added_updated_at]
    changes_tree[:attributes] += [{ type: :timestamps, kind: :added }]
  end

  changes_tree[:attributes].each do |attribute|
    persisted_attribute = persisted_attributes[attribute[:name]]
    if persisted_attribute && attribute[:options][:default].nil? && persisted_attribute[:options][:default]
      changes_tree[:attributes] += [{ name: attribute[:name], kind: :removed_default }]
    end
  end

  changes_tree
end

#has_and_belongs_to_many(name, **options) ⇒ Object Also known as: habtm



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
# File 'lib/whiteprint/adapters/active_record.rb', line 100

def has_and_belongs_to_many(name, **options)
  super(name, **options.merge(virtual: true))
  model.send(:has_and_belongs_to_many, name.to_sym, **options) unless model.reflect_on_association(name)

  association = model.reflect_on_association(name)

  Class.new do
    include Whiteprint::Model

    @association = association
    @join_table  = association.join_table

    whiteprint(adapter: :has_and_belongs_to_many, id: false, timestamps: false) do
      integer association.foreign_key
      integer association.association_foreign_key
    end

    class << self
      def name
        "#{@join_table}_habtm_model"
      end

      def table_name
        @join_table
      end

      def table_exists?
        ::ActiveRecord::Base.connection.table_exists?(table_name)
      end
    end
  end
end

#migration(name) ⇒ Object



134
135
136
# File 'lib/whiteprint/adapters/active_record.rb', line 134

def migration(name)
  self.class.migration(name, [changes_tree])
end

#options_from_column(column) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/whiteprint/adapters/active_record.rb', line 138

def options_from_column(column)
  [:name, :type, *Whiteprint.config.persisted_attribute_options.keys].map do |option|
    association_by_foreign_key = find_association_by_foreign_key(column)
    overridden_name            = association_by_foreign_key && association_by_foreign_key.name || column.name
    current_attribute          = attributes[overridden_name]

    next {name: overridden_name}               if option == :name        && association_by_foreign_key
    next {type: :references}                   if option == :type        && association_by_foreign_key
    next {polymorphic: true}                   if option == :polymorphic && association_by_foreign_key && model.column_names.include?(association_by_foreign_key.foreign_type)
    next                                       unless column.respond_to?(option)
    next {default: current_attribute.default}  if option == :default && current_attribute && current_attribute.default.is_a?(Symbol)

    value = column.send(option)
    value = column.type_cast_from_database(value) if option == :default
    next if value == Whiteprint.config.persisted_attribute_options[option]
    { option => value }
  end.compact.inject(&:merge)
end

#persisted_attributesObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/whiteprint/adapters/active_record.rb', line 157

def persisted_attributes
  attributes = Whiteprint::Attributes.new
  return attributes unless table_exists?
  model.columns.each do |column|
    next if find_association_by_foreign_type(column)

    attributes.add options_from_column(column)
  end
  attributes.for_persisted
end

#references(name, **options) ⇒ Object



168
169
170
171
172
# File 'lib/whiteprint/adapters/active_record.rb', line 168

def references(name, **options)
  super
  return unless @auto_belongs_to
  model.send :belongs_to, name.to_sym, **options.slice(*BELONGS_TO_OPTIONS)
end

#table_exists?Boolean

Returns:

  • (Boolean)


174
175
176
177
# File 'lib/whiteprint/adapters/active_record.rb', line 174

def table_exists?
  model.connection.schema_cache.clear!
  model.connection.table_exists?(table_name)
end