Last active
July 31, 2023 16:55
-
-
Save fractaledmind/a089f2b2a8a3d26c78835fffefdea42a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /lib/rails_ext/generated_attribute.rb | |
# Here, we patch the GeneratedAttribute class to add `richer_text` as a field type, which behaves much the same as the `rich_text` type. | |
# We will patch the Model generator as well to tweak the ActiveRecord model generated when this type is used | |
require 'rails/generators/generated_attribute' | |
module Rails | |
module Generators | |
class GeneratedAttribute | |
DEFAULT_TYPES = %w( | |
attachment | |
attachments | |
belongs_to | |
boolean | |
date | |
datetime | |
decimal | |
digest | |
float | |
integer | |
references | |
rich_text | |
string | |
text | |
time | |
timestamp | |
token | |
richer_text | |
) | |
def field_type | |
@field_type ||= case type | |
when :integer then :number_field | |
when :float, :decimal then :text_field | |
when :time then :time_field | |
when :datetime, :timestamp then :datetime_field | |
when :date then :date_field | |
when :text then :text_area | |
when :rich_text then :rich_text_area | |
when :boolean then :check_box | |
when :attachment, :attachments then :file_field | |
when :richer_text then :rich_text_area | |
else | |
:text_field | |
end | |
end | |
def default | |
@default ||= case type | |
when :integer then 1 | |
when :float then 1.5 | |
when :decimal then "9.99" | |
when :datetime, :timestamp, :time then Time.now.to_fs(:db) | |
when :date then Date.today.to_fs(:db) | |
when :string then name == "type" ? "" : "MyString" | |
when :text then "MyText" | |
when :boolean then false | |
when :references, :belongs_to, | |
:attachment, :attachments, | |
:rich_text, :richer_text then nil | |
else | |
"" | |
end | |
end | |
def richer_text? | |
type == :richer_text | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /lib/rails/templates/active_record/model/model.rb.tt | |
# Here, we patch the ActiveRecord model generator template to do something with attributes that use the `richer_text` attribute type | |
<% 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(&:richer_text?).each do |attribute| -%> | |
has_richer_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 -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /config/intializers/rails_extensions.rb | |
Dir[File.join(Rails.root, "/lib/rails_ext/**/*.rb")].sort.each { |f| require f } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment