Add ActiveRecord templates
This commit is contained in:
parent
8cf37e1b64
commit
93e19cb962
5 changed files with 123 additions and 0 deletions
|
@ -0,0 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
<% module_namespacing do -%>
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
end
|
||||
<% end -%>
|
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
||||
def change
|
||||
create_table :<%= table_name %><%= primary_key_type %> do |t|
|
||||
<% attributes.each do |attribute| -%>
|
||||
<% if attribute.password_digest? -%>
|
||||
t.string :password_digest<%= attribute.inject_options %>
|
||||
<% elsif attribute.token? -%>
|
||||
t.string :<%= attribute.name %><%= attribute.inject_options %>
|
||||
<% elsif attribute.reference? -%>
|
||||
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
|
||||
<% elsif !attribute.virtual? -%>
|
||||
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
<% if options[:timestamps] %>
|
||||
t.timestamps
|
||||
<% end -%>
|
||||
end
|
||||
<% attributes.select(&:token?).each do |attribute| -%>
|
||||
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
|
||||
<% end -%>
|
||||
<% attributes_with_index.each do |attribute| -%>
|
||||
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
||||
<% end -%>
|
||||
end
|
||||
end
|
51
lib/templates/active_record/migration/migration.rb.tt
Normal file
51
lib/templates/active_record/migration/migration.rb.tt
Normal file
|
@ -0,0 +1,51 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
||||
<%- if migration_action == 'add' -%>
|
||||
def change
|
||||
<% attributes.each do |attribute| -%>
|
||||
<%- if attribute.reference? -%>
|
||||
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
|
||||
<%- elsif attribute.token? -%>
|
||||
add_column :<%= table_name %>, :<%= attribute.name %>, :string<%= attribute.inject_options %>
|
||||
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
|
||||
<%- elsif !attribute.virtual? -%>
|
||||
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
||||
<%- if attribute.has_index? -%>
|
||||
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
||||
<%- end -%>
|
||||
<%- end -%>
|
||||
<%- end -%>
|
||||
end
|
||||
<%- elsif migration_action == 'join' -%>
|
||||
def change
|
||||
create_join_table :<%= join_tables.first %>, :<%= join_tables.second %> do |t|
|
||||
<%- attributes.each do |attribute| -%>
|
||||
<%- if attribute.reference? -%>
|
||||
t.references :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
|
||||
<%- elsif !attribute.virtual? -%>
|
||||
<%= '# ' unless attribute.has_index? -%>t.index <%= attribute.index_name %><%= attribute.inject_index_options %>
|
||||
<%- end -%>
|
||||
<%- end -%>
|
||||
end
|
||||
end
|
||||
<%- else -%>
|
||||
def change
|
||||
<% attributes.each do |attribute| -%>
|
||||
<%- if migration_action -%>
|
||||
<%- if attribute.reference? -%>
|
||||
remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
|
||||
<%- else -%>
|
||||
<%- if attribute.has_index? -%>
|
||||
remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
||||
<%- end -%>
|
||||
<%- if !attribute.virtual? -%>
|
||||
remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
||||
<%- end -%>
|
||||
<%- end -%>
|
||||
<%- end -%>
|
||||
<%- end -%>
|
||||
end
|
||||
<%- end -%>
|
||||
end
|
25
lib/templates/active_record/model/model.rb.tt
Normal file
25
lib/templates/active_record/model/model.rb.tt
Normal file
|
@ -0,0 +1,25 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
<% module_namespacing do -%>
|
||||
class <%= class_name %> < <%= parent_class_name.classify %>
|
||||
<% attributes.select(&:reference?).each do |attribute| -%>
|
||||
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
|
||||
<% end -%>
|
||||
<% attributes.select(&:rich_text?).each do |attribute| -%>
|
||||
has_rich_text :<%= attribute.name %>
|
||||
<% end -%>
|
||||
<% attributes.select(&:attachment?).each do |attribute| -%>
|
||||
has_one_attached :<%= attribute.name %>
|
||||
<% end -%>
|
||||
<% attributes.select(&:attachments?).each do |attribute| -%>
|
||||
has_many_attached :<%= attribute.name %>
|
||||
<% end -%>
|
||||
<% attributes.select(&:token?).each do |attribute| -%>
|
||||
has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
|
||||
<% end -%>
|
||||
<% if attributes.any?(&:password_digest?) -%>
|
||||
has_secure_password
|
||||
<% end -%>
|
||||
end
|
||||
<% end -%>
|
10
lib/templates/active_record/model/module.rb.tt
Normal file
10
lib/templates/active_record/model/module.rb.tt
Normal file
|
@ -0,0 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
<% module_namespacing do -%>
|
||||
module <%= class_path.map(&:camelize).join('::') %>
|
||||
def self.table_name_prefix
|
||||
'<%= namespaced? ? namespaced_class_path.join('_') : class_path.join('_') %>_'
|
||||
end
|
||||
end
|
||||
<% end -%>
|
Loading…
Reference in a new issue