Sha256: 8b8cd11bb9d4557567b08d308fbfbb1c57745311dafc8c0a3c0a09b963da9ada

Contents?: true

Size: 1.45 KB

Versions: 216

Compression:

Stored size: 1.45 KB

Contents

# HasManyRichTexts
#
# Mark your model with 'has_many_rich_texts'
# Then it will automatically create a region when using a method named rich_text_*
# object.rich_text_body = "<p>Stuff</p>"
# object.rich_text_body => ActionText::RichText<name="body" body="<p>Stuff</p>">

module HasManyRichTexts
  extend ActiveSupport::Concern

  module Base
    def has_many_rich_texts(options = nil)
      include ::HasManyRichTexts
    end
  end

  included do
    has_many :rich_texts, class_name: 'ActionText::RichText', as: :record, inverse_of: :record, dependent: :destroy
    accepts_nested_attributes_for :rich_texts, allow_destroy: true
  end

  module ClassMethods
  end

  # Find or build
  def rich_text(name)
    name = name.to_s
    rich_texts.find { |rt| rt.name == name } || rich_texts.build(name: name)
  end

  def assign_rich_text_body(name, body)
    rich_text(name).assign_attributes(body: body)
  end

  # Prevents an ActiveModel::UnknownAttributeError
  # https://github.com/rails/rails/blob/main/activemodel/lib/active_model/attribute_assignment.rb#L48
  def respond_to?(*args)
    args.first.to_s.start_with?('rich_text_') ? true : super
  end

  def method_missing(method, *args, &block)
    return super unless method.to_s.start_with?('rich_text_')

    method = method.to_s
    name = method.chomp('=').sub('rich_text_', '')

    if method.end_with?('=')
      send(:assign_rich_text_body, name, *args)
    else
      send(:rich_text, name, *args)
    end
  end

end

Version data entries

216 entries across 216 versions & 1 rubygems

Version Path
effective_resources-2.9.5 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.9.4 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.9.3 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.9.2 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.9.1 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.9.0 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.9 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.8 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.7 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.6 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.5 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.4 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.3 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.2 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.1 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.8.0 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.7.20 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.7.19 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.7.18 app/models/concerns/has_many_rich_texts.rb
effective_resources-2.7.17 app/models/concerns/has_many_rich_texts.rb