Class: Workarea::Content
- Inherits:
-
Object
- Object
- Workarea::Content
- Includes:
- ApplicationDocument, Commentable, Releasable
- Defined in:
- app/models/workarea/content.rb,
app/models/workarea/content/block.rb,
app/models/workarea/content/email.rb,
app/models/workarea/content/field.rb,
app/models/workarea/content/preset.rb,
app/models/workarea/content/fieldset.rb,
app/models/workarea/content/block_name.rb,
app/models/workarea/content/block_type.rb,
app/models/workarea/content/fields/url.rb,
app/models/workarea/content/block_draft.rb,
app/models/workarea/content/fields/text.rb,
app/models/workarea/content/asset_lookup.rb,
app/models/workarea/content/fields/asset.rb,
app/models/workarea/content/fields/color.rb,
app/models/workarea/content/fields/range.rb,
app/models/workarea/content/fields/string.rb,
app/models/workarea/content/fields/boolean.rb,
app/models/workarea/content/fields/integer.rb,
app/models/workarea/content/fields/options.rb,
app/models/workarea/content/fields/category.rb,
app/models/workarea/content/fields/products.rb,
app/models/workarea/content/fields/taxonomy.rb,
app/models/workarea/content/fields/breakpoints.rb,
app/models/workarea/content/block_type_definition.rb
Defined Under Namespace
Modules: AssetLookup, Fields Classes: Asset, Block, BlockDraft, BlockName, BlockType, BlockTypeDefinition, Email, Field, Fieldset, Page, Preset
Class Method Summary collapse
-
.define_block_types(&block) ⇒ Object
Define block types for use by administrators.
-
.for(object) ⇒ Content
Find Content for a given object.
- .from_block(block_id) ⇒ Content
Instance Method Summary collapse
-
#all_text ⇒ Array<String>
Get an array of all text strings associated as part of this content.
-
#blocks_for(area) ⇒ Array<Block>
Find an array of active blocks for a given area.
-
#home_page? ⇒ Boolean
Whether this is home page content, which is a special case.
-
#layout? ⇒ Boolean
Whether this is layout content, which is a special case.
-
#name ⇒ String
The name for this content, returns the name of the contentable if that is present.
-
#slug ⇒ String
Slug for the type of object this content is attached to.
-
#system? ⇒ Boolean
Whether this content belongs to a system page as opposed to a contentable model.
Methods included from Commentable
#add_subscription, #remove_subscription
Methods included from Releasable
#activates_with_current_release?, #releasable?, #release_changes, #save_changeset, #skip_changeset
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list
Class Method Details
.define_block_types(&block) ⇒ Object
Define block types for use by administrators. A block type represents a row of content on the storefront, self-contained with its own styles and responsive logic.
Defining new block types
Workarea::Content.define_block_types do
# Create a new block type called 2 Column Text
# The ID or slug for this type will be :2_column_text
block_type '2 Column Text' do
# Set a description, which will be shown to admin users when
# selecting a block type
description 'Provides 2 columns of text'
# Allows custom specification of which icon to use to display this
# block type in the admin when selecting a new block. The default
# is workarea/admin/content_block_types/#{block_type_id}
icon 'workarea/admin/content_block_types/columns'
# Tags are used for filtering content block types in the admin when
# creating a new block and selecting its type
%w(columns text)
# You can also specify a custom view model to be used in the store
# front when rendering. You could even share view models for
# different blocks
view_model 'Workarea::Storefront::ContentBlocks::ColumnsViewModel'
# If your block type requires developer-facing configuration, you
# you can specify any arbitrary attributes and they will be added
# to the #config hash on the block type. For example, configuration
# values tied to site design.
height 960
width 470
# Use fieldset to group fields together for admin display. The
# fieldset has no other use.
fieldset 'Left Column' do
# A field corresponds to one input in the admin and one key in
# the Workarea::Content::Block#data hash. It will be referenced
# by a systemized version of the name.
# The second argument must be a type. Out-of-the-box valid types
# include :asset, :category, :options, :products, :rich_text,
# :string, and :url.
# Options can be specific to the field type. All field types
# support :default and :required.
field 'Left Column Text', :text, required: true, default: 'Left Column'
end
fieldset 'Right Column' do
field 'Right Column Text', :text, required: true, default: 'Right Column'
end
end
end
Overriding values for existing block types
To allow full customization, all details about a block type and its fields can be overridden. Here’s an example of overriding details on the Product List block type which ships out of the box.
Workarea::Content.define_block_types do
# Open the Product List block up again
block_type 'Product List' do
# Override the default on Title
field 'Title', :string, default: 'Staff Picks'
# Add a new field called description
field 'Description', :text, default: 'Top picks by our staff
# No need to touch other fields, they remain the same
end
end
Admin UI
The admin UI for the block will be automatically generated based on the fields provided.
139 140 141 142 |
# File 'app/models/workarea/content.rb', line 139 def self.define_block_types(&block) definition = BlockTypeDefinition.new definition.instance_eval(&block) end |
.for(object) ⇒ Content
Find Workarea::Content for a given object. Object can be string or model (polymorphic).
40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/workarea/content.rb', line 40 def self.for(object) if object.is_a?(String) find_or_create_by(name: object.titleize) else find_or_create_by( contentable_id: object.try(:id), contentable_type: object.try(:class) ) end end |
.from_block(block_id) ⇒ Content
Find the Workarea::Content from a Block id
55 56 57 58 |
# File 'app/models/workarea/content.rb', line 55 def self.from_block(block_id) block_id = BSON::ObjectId.from_string(block_id.to_s) find_by('blocks._id' => block_id) rescue nil end |
Instance Method Details
#all_text ⇒ Array<String>
Get an array of all text strings associated as part of this content. Used in indexing content for search.
213 214 215 |
# File 'app/models/workarea/content.rb', line 213 def all_text blocks.map { |b| b.data.values }.flatten end |
#blocks_for(area) ⇒ Array<Block>
Find an array of active blocks for a given area.
204 205 206 |
# File 'app/models/workarea/content.rb', line 204 def blocks_for(area) blocks.where(area: area).all end |
#home_page? ⇒ Boolean
Whether this is home page content, which is a special case. Used when linking to contentable.
185 186 187 |
# File 'app/models/workarea/content.rb', line 185 def home_page? name =~ /home/i end |
#layout? ⇒ Boolean
Whether this is layout content, which is a special case. Used when linking to contentable.
195 196 197 |
# File 'app/models/workarea/content.rb', line 195 def layout? name =~ /layout/i end |
#name ⇒ String
The name for this content, returns the name of the contentable if that is present.
149 150 151 152 153 154 155 |
# File 'app/models/workarea/content.rb', line 149 def name if !system? && contentable.present? contentable.name else read_attribute(:name) end end |
#slug ⇒ String
Slug for the type of object this content is attached to.
171 172 173 174 175 176 177 |
# File 'app/models/workarea/content.rb', line 171 def slug if system? name.slugify else contentable_type.to_s.demodulize.underscore end end |
#system? ⇒ Boolean
Whether this content belongs to a system page as opposed to a contentable model.
162 163 164 |
# File 'app/models/workarea/content.rb', line 162 def system? contentable_type.blank? end |