Module: HasManyRichTexts

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/has_many_rich_texts.rb

Overview

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>”>

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/concerns/has_many_rich_texts.rb', line 41

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

Instance Method Details

#assign_rich_text_body(name, body) ⇒ Object



31
32
33
# File 'app/models/concerns/has_many_rich_texts.rb', line 31

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

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/concerns/has_many_rich_texts.rb', line 37

def respond_to?(*args)
  args.first.to_s.start_with?('rich_text_') ? true : super
end

#rich_text(name) ⇒ Object

Find or build



26
27
28
29
# File 'app/models/concerns/has_many_rich_texts.rb', line 26

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