Class: ActionText::RichText
- Inherits:
-
Record
- Object
- ActiveRecord::Base
- Record
- ActionText::RichText
- Defined in:
- actiontext/app/models/action_text/rich_text.rb
Overview
# Action Text RichText
The RichText record holds the content produced by the Trix editor in a serialized ‘body` attribute. It also holds all the references to the embedded files, which are stored using Active Storage. This record is then associated with the Active Record model the application desires to have rich text content using the `has_rich_text` class method.
class Message < ActiveRecord::Base
has_rich_text :content
end
= Message.create!(content: "<h1>Funny times!</h1>")
.content #=> #<ActionText::RichText....
.content.to_s # => "<h1>Funny times!</h1>"
.content.to_plain_text # => "Funny times!"
= Message.create!(content: "<div onclick='action()'>safe<script>unsafe</script></div>")
.content #=> #<ActionText::RichText....
.content.to_s # => "<div>safeunsafe</div>"
.content.to_plain_text # => "safeunsafe"
Direct Known Subclasses
Constant Summary
Constants included from ActiveRecord::ConnectionHandling
ActiveRecord::ConnectionHandling::DEFAULT_ENV, ActiveRecord::ConnectionHandling::RAILS_ENV
Constants included from ActiveRecord::Querying
ActiveRecord::Querying::QUERYING_METHODS
Constants included from ActiveRecord::SecureToken
ActiveRecord::SecureToken::MINIMUM_TOKEN_LENGTH
Constants included from ActiveRecord::Transactions
ActiveRecord::Transactions::ACTIONS
Constants included from ActiveModel::SecurePassword
ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED
Constants included from ActiveRecord::Callbacks
ActiveRecord::Callbacks::CALLBACKS
Constants included from ActiveRecord::AttributeMethods
ActiveRecord::AttributeMethods::RESTRICTED_CLASS_METHODS
Constants included from ActiveModel::AttributeMethods
ActiveModel::AttributeMethods::CALL_COMPILABLE_REGEXP, ActiveModel::AttributeMethods::NAME_COMPILABLE_REGEXP
Instance Attribute Summary
Attributes included from ActiveRecord::ConnectionHandling
#connection_specification_name
Attributes included from ActiveRecord::Delegation::DelegateCache
Attributes included from ActiveRecord::Transactions
#_new_record_before_last_commit
Attributes included from ActiveRecord::Core
Instance Method Summary collapse
-
#body ⇒ Object
:method: to_s.
-
#embeds ⇒ Object
:method: embeds.
-
#record ⇒ Object
:method: record.
-
#to_plain_text ⇒ Object
Returns a plain-text version of the markup contained by the ‘body` attribute, with tags removed but HTML entities encoded.
-
#to_trix_html ⇒ Object
Returns the ‘body` attribute in a format that makes it editable in the Trix editor.
Methods included from ActiveSupport::Benchmarkable
Methods included from ActiveSupport::DescendantsTracker
clear, #descendants, descendants, disable_clear!, reject!, subclasses
Methods included from ActiveRecord::ConnectionHandling
#adapter_class, #clear_cache!, #clear_query_caches_for_current_thread, #connected?, #connected_to, #connected_to?, #connected_to_all_shards, #connected_to_many, #connecting_to, #connection, #connection_db_config, #connection_pool, #connects_to, #establish_connection, #lease_connection, #primary_class?, #prohibit_shard_swapping, #release_connection, #remove_connection, #retrieve_connection, #schema_cache, #shard_keys, #shard_swapping_prohibited?, #sharded?, #while_preventing_writes, #with_connection
Methods included from ActiveRecord::QueryCache::ClassMethods
Methods included from ActiveRecord::Querying
#_load_from_sql, #_query_by_sql, #async_count_by_sql, #async_find_by_sql, #count_by_sql, #find_by_sql
Methods included from ActiveRecord::Translation
#i18n_scope, #lookup_ancestors
Methods included from ActiveRecord::DelegatedType
Methods included from ActiveRecord::Explain
#collecting_queries_for_explain, #exec_explain
Methods included from ActiveRecord::Enum
Methods included from ActiveRecord::Delegation::DelegateCache
#generate_relation_method, #inherited, #initialize_relation_delegate_cache, #relation_delegate_class
Methods included from ActiveRecord::Aggregations::ClassMethods
Methods included from ActiveRecord::Marshalling::Methods
#_marshal_dump_7_1, #marshal_load
Methods included from ActiveRecord::Normalization
Methods included from ActiveSupport::Concern
#append_features, #class_methods, extended, #included, #prepend_features, #prepended
Methods included from ActiveRecord::Suppressor
Methods included from ActiveRecord::SignedId
Methods included from ActiveRecord::TokenFor
Methods included from ActiveRecord::Serialization
Methods included from ActiveModel::Serializers::JSON
Methods included from ActiveModel::Serialization
Methods included from ActiveRecord::Reflection
add_aggregate_reflection, add_reflection, create
Methods included from ActiveRecord::NoTouching
applied_to?, apply_to, #no_touching?, #touch, #touch_later
Methods included from ActiveRecord::TouchLater
#before_committed!, #touch, #touch_later
Methods included from ActiveRecord::Transactions
#before_committed!, #committed!, #destroy, #rolledback!, #save, #save!, #touch, #transaction, #trigger_transactional_callbacks?, #with_transaction_returning_status
Methods included from ActiveRecord::NestedAttributes
Methods included from ActiveRecord::AutosaveAssociation
#autosaving_belongs_to_for?, #changed_for_autosave?, #destroyed_by_association, #destroyed_by_association=, #mark_for_destruction, #marked_for_destruction?, #reload, #validating_belongs_to_for?
Methods included from ActiveRecord::Associations
#association, #association_cached?, eager_load!, #initialize_dup
Methods included from ActiveSupport::Autoload
#autoload, #autoload_at, #autoload_under, #eager_autoload, #eager_load!
Methods included from ActiveRecord::Timestamp
Methods included from ActiveRecord::Callbacks
Methods included from ActiveRecord::AttributeMethods
#[], #[]=, #_has_attribute?, #accessed_fields, #attribute_for_inspect, #attribute_names, #attribute_present?, #attributes, dangerous_attribute_methods, #has_attribute?, #respond_to?
Methods included from ActiveModel::AttributeMethods
#attribute_missing, #method_missing, #respond_to?, #respond_to_without_attributes?
Methods included from ActiveRecord::Encryption::EncryptableRecord
#ciphertext_for, #decrypt, #encrypt, #encrypted_attribute?
Methods included from ActiveRecord::Locking::Pessimistic
Methods included from ActiveRecord::Locking::Optimistic
#increment!, #initialize_dup, #locking_enabled?
Methods included from ActiveRecord::Validations
#custom_validation_context?, #save, #save!, #valid?
Methods included from ActiveRecord::Integration
#cache_key, #cache_key_with_version, #cache_version, #to_param
Methods included from ActiveRecord::Scoping
#initialize_internals_callback, #populate_with_current_scope_attributes
Methods included from ActiveRecord::Inheritance
Methods included from ActiveRecord::ModelSchema
Methods included from ActiveRecord::Persistence
#becomes, #becomes!, #decrement, #decrement!, #delete, #destroy, #destroy!, #destroyed?, #increment, #increment!, #new_record?, #persisted?, #previously_new_record?, #previously_persisted?, #reload, #save, #save!, #toggle, #toggle!, #touch, #update, #update!, #update_attribute, #update_attribute!, #update_column, #update_columns
Methods included from ActiveRecord::Core
#<=>, #==, #blank?, #connection_handler, #encode_with, #freeze, #frozen?, #full_inspect, #hash, #init_attributes, #init_with, #init_with_attributes, #initialize, #initialize_dup, #inspect, #present?, #pretty_print, #readonly!, #readonly?, #strict_loading!, #strict_loading?, #strict_loading_all?, #strict_loading_n_plus_one_only?
Methods included from ActiveModel::Access
Methods included from ActiveModel::API
Methods included from ActiveModel::Conversion
#to_key, #to_model, #to_param, #to_partial_path
Methods included from ActiveModel::Validations
#errors, #freeze, #initialize_dup, #invalid?, #valid?, #validate!, #validates_with, #validation_context
Methods included from ActiveModel::AttributeAssignment
#assign_attributes, #attribute_writer_missing
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveRecord::AttributeMethods
Instance Method Details
#body ⇒ Object
:method: to_s
Safely transforms RichText into an HTML String.
= Message.create!(content: "<h1>Funny times!</h1>")
.content.to_s # => "<h1>Funny times!</h1>"
= Message.create!(content: "<div onclick='action()'>safe<script>unsafe</script></div>")
.content.to_s # => "<div>safeunsafe</div>"
39 |
# File 'actiontext/app/models/action_text/rich_text.rb', line 39 serialize :body, coder: ActionText::Content |
#embeds ⇒ Object
:method: embeds
Returns the ‘ActiveStorage::Blob`s of the embedded files.
52 |
# File 'actiontext/app/models/action_text/rich_text.rb', line 52 has_many_attached :embeds |
#record ⇒ Object
:method: record
Returns the associated record.
46 |
# File 'actiontext/app/models/action_text/rich_text.rb', line 46 belongs_to :record, polymorphic: true, touch: true |
#to_plain_text ⇒ Object
Returns a plain-text version of the markup contained by the ‘body` attribute, with tags removed but HTML entities encoded.
= Message.create!(content: "<h1>Funny times!</h1>")
.content.to_plain_text # => "Funny times!"
NOTE: that the returned string is not HTML safe and should not be rendered in browsers.
= Message.create!(content: "<script>alert()</script>")
.content.to_plain_text # => "<script>alert()</script>"
69 70 71 |
# File 'actiontext/app/models/action_text/rich_text.rb', line 69 def to_plain_text body&.to_plain_text.to_s end |
#to_trix_html ⇒ Object
Returns the ‘body` attribute in a format that makes it editable in the Trix editor. Previews of attachments are rendered inline.
content = "<h1>Funny Times!</h1><figure data-trix-attachment='{\"sgid\":\"..."\}'></figure>"
message = Message.create!(content: content)
message.content.to_trix_html # =>
# <div class="trix-content">
# <h1>Funny times!</h1>
# <figure data-trix-attachment='{\"sgid\":\"..."\}'>
# <img src="http://example.org/rails/active_storage/.../funny.jpg">
# </figure>
# </div>
85 86 87 |
# File 'actiontext/app/models/action_text/rich_text.rb', line 85 def to_trix_html body&.to_trix_html end |