Class: Decidim::ContentBlock
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::ContentBlock
- Includes:
- Publicable
- Defined in:
- app/models/decidim/content_block.rb
Overview
this class represents a content block manifest instance in the DB. Check the docs on ‘ContentBlockRegistry` and `ContentBlockManifest` for more info.
Instance Attribute Summary collapse
-
#in_preview ⇒ Object
Returns the value of attribute in_preview.
Class Method Summary collapse
-
.for_scope(scope, organization:) ⇒ Object
Public: finds the published content blocks for the given scope and organization.
Instance Method Summary collapse
-
#images_container ⇒ Object
Public: Holds access to the images related to the content block.
- #manifest ⇒ Object
- #reload ⇒ Object
-
#settings ⇒ Object
Public: Uses the ‘SettingsManifest` class to generate a settings schema and fill it with the content blocks current settings.
Methods included from Publicable
#previously_published?, #publish!, #published?, #unpublish!
Instance Attribute Details
#in_preview ⇒ Object
Returns the value of attribute in_preview.
9 10 11 |
# File 'app/models/decidim/content_block.rb', line 9 def in_preview @in_preview end |
Class Method Details
.for_scope(scope, organization:) ⇒ Object
Public: finds the published content blocks for the given scope and organization. Returns them ordered by ascending weight (lowest first).
23 24 25 26 |
# File 'app/models/decidim/content_block.rb', line 23 def self.for_scope(scope, organization:) where(organization: organization, scope_name: scope) .order(weight: :asc) end |
Instance Method Details
#images_container ⇒ Object
Public: Holds access to the images related to the content block. This method generates a dynamic class that encapsulates the uploaders for the content block images, and eases the access to them. It’s a little bit hacky, but it’s the only way I could come up with in order to let content block images have different uploaders.
Examples:
# This will process the image with the uploader defined at the
# manifest, upload it and save the record.
content_block.images_container.my_image = params[:my_image]
content_block.save!
# This is how you can access the image data, just like with any other
# uploader field. You can use the uploader variants too.
content_block.images_container.attached_uploader(:my_image).path
content_block.images_container.attached_uploader(:my_image).path(variant: :big)
# This will delete the attached image
content_block.images_container.my_image = nil
content_block.save!
Returns an object that responds to the image names defined in the content block manifest.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/models/decidim/content_block.rb', line 72 def images_container return @images_container if @images_container = @images_container = Class.new do include ActiveModel::Validations include Decidim::HasUploadValidations cattr_accessor :manifest_attachments attr_reader :content_block def self.name to_s.camelize end delegate :id, :organization, to: :content_block def initialize(content_block) @content_block = content_block end .each do |name, | validates_upload name, uploader: .uploader define_method(name) do .file end define_method("#{name}=") do |file| .file = file end end def attached_uploader(name) return if [name].blank? [name].attached_uploader(:file) end def save return unless content_block.persisted? .values.map(&:save).all? end end @images_container. = @images_container = @images_container.new(self) end |
#manifest ⇒ Object
28 29 30 |
# File 'app/models/decidim/content_block.rb', line 28 def manifest @manifest ||= Decidim.content_blocks.for(scope_name).find { |manifest| manifest.name.to_s == manifest_name } end |
#reload ⇒ Object
42 43 44 45 46 |
# File 'app/models/decidim/content_block.rb', line 42 def reload(*) @manifest_attachments = nil @images_container = nil super end |
#settings ⇒ Object
Public: Uses the ‘SettingsManifest` class to generate a settings schema and fill it with the content blocks current settings. This eases the access to those settings values.
Returns an object that responds to the settings defined in the content block manifest.
38 39 40 |
# File 'app/models/decidim/content_block.rb', line 38 def settings manifest.settings.schema.new(self[:settings]) end |