Class: Whiteprint::Adapters::ActiveRecord
- Inherits:
-
Base
- Object
- Base
- Whiteprint::Adapters::ActiveRecord
show all
- Defined in:
- lib/whiteprint/adapters/active_record.rb
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.
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/whiteprint/adapters/active_record.rb', line 46
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
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/whiteprint/adapters/active_record.rb', line 167
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
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
|
# 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"
File.open(File.join(Whiteprint.config.migration_path, filename), 'w') do |f|
f.write migration(name, trees)
end
end
|
.migration(name, trees) ⇒ Object
35
36
37
|
# File 'lib/whiteprint/adapters/active_record.rb', line 35
def migration(name, trees)
"class #{camelize(name)} < ActiveRecord::Migration\n def change\n" + transform(trees) + " end\nend\n"
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
57
58
59
60
|
# File 'lib/whiteprint/adapters/active_record.rb', line 57
def accessor(name, options)
@attributes.add(name: name, type: :accessor, virtual: true, **options)
model.send :attr_accessor, name
end
|
#changes_tree ⇒ 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
|
# File 'lib/whiteprint/adapters/active_record.rb', line 62
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
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
|
# File 'lib/whiteprint/adapters/active_record.rb', line 88
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
122
123
124
|
# File 'lib/whiteprint/adapters/active_record.rb', line 122
def migration(name)
self.class.migration(name, [changes_tree])
end
|
#options_from_column(column) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/whiteprint/adapters/active_record.rb', line 126
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_attributes ⇒ Object
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/whiteprint/adapters/active_record.rb', line 145
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
156
157
158
159
160
|
# File 'lib/whiteprint/adapters/active_record.rb', line 156
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
162
163
164
165
|
# File 'lib/whiteprint/adapters/active_record.rb', line 162
def table_exists?
model.connection.schema_cache.clear!
model.connection.table_exists?(table_name)
end
|