Class: ContentsCore::Block
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ContentsCore::Block
- Defined in:
- app/models/contents_core/block.rb
Constant Summary collapse
- EMPTY_DATA =
OpenStruct.new( { data: '' } )
Class Method Summary collapse
- .block_list ⇒ Object
- .block_types ⇒ Object
- .init_items(block, items) ⇒ Object
- .permitted_attributes ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #attr_id ⇒ Object
- #blocks_collection ⇒ Object
- #children_type ⇒ Object
- #config ⇒ Object
- #create_item(item_type, item_name = nil) ⇒ Object
- #editable ⇒ Object
-
#get(name) ⇒ Object
Returns an item by name.
- #get_item(name) ⇒ Object
- #has_children? ⇒ Boolean
- #has_parent? ⇒ Boolean
-
#initialize(params = {}) ⇒ Block
constructor
after_validation :on_after_validation.
- #is_sub_block? ⇒ Boolean
- #items_collection ⇒ Object
- #on_after_create ⇒ Object
- #on_before_create ⇒ Object
- #props ⇒ Object
- #set(name, value) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Block
after_validation :on_after_validation
field :block_type, type: String, default: ‘text’ field :name, type: String, default: ” field :position, type: Integer, default: 0 field :published, type: Mongoid::Boolean, default: true field :_init, type: Mongoid::Boolean, default: false
embedded_in :parent, polymorphic: true
embeds_many :cc_blocks, cascade_callbacks: true, order: :position.desc, class_name: ‘ContentsCore::Block’ embeds_many :items, cascade_callbacks: true, class_name: ‘ContentsCore::Item’
accepts_nested_attributes_for :cc_blocks, allow_destroy: true accepts_nested_attributes_for :items
# scope :published, -> { where( published: true ) unless ApplicationController.edit_mode }
54 55 56 57 58 |
# File 'app/models/contents_core/block.rb', line 54 def initialize( params = {} ) super self.group = config[:group] self.block_type = parent.config[:children_type] if params[:block_type].nil? && self.parent_type == 'ContentsCore::Block' end |
Class Method Details
.block_list ⇒ Object
187 188 189 |
# File 'app/models/contents_core/block.rb', line 187 def self.block_list @@block_list ||= ContentsCore.config[:cc_blocks].map{|k, v| [v[:name], k.to_s] unless v[:child_only]}.compact.sort_by{|b| b[0]} end |
.block_types ⇒ Object
191 192 193 |
# File 'app/models/contents_core/block.rb', line 191 def self.block_types @@block_types ||= ContentsCore.config[:cc_blocks].keys end |
.init_items(block, items) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/models/contents_core/block.rb', line 195 def self.init_items( block, items ) items.each do |name, type| t = type.to_sym if type.to_s.start_with? 'item_' c = 'ContentsCore::' + ActiveSupport::Inflector.camelize( t ) begin model = c.constantize rescue Exception => e Rails.logger.error '[ERROR] ContentsCore - init_items: ' + e. model = false end block.items << model.new( name: name ).init if model elsif Block::block_types.include? t.to_sym block.cc_blocks << ( cmp = Block.new( block_type: t, name: name ) ) end end if items end |
.permitted_attributes ⇒ Object
213 214 215 |
# File 'app/models/contents_core/block.rb', line 213 def self.permitted_attributes [ :id, :name, :block_type, :position, :_destroy, items_attributes: [ :id ] + Item::permitted_attributes, cc_blocks_attributes: [ :id, :name, :block_type, items_attributes: [ :id ] + Item::permitted_attributes ] ] end |
Instance Method Details
#as_json ⇒ Object
60 61 62 63 |
# File 'app/models/contents_core/block.rb', line 60 def as_json # binding.pry super({ only: [:id, :block_type, :name, :group, :position, :published], methods: [:blocks_collection, :items_collection]}.merge( || {})) end |
#attr_id ⇒ Object
65 66 67 |
# File 'app/models/contents_core/block.rb', line 65 def attr_id "#{self.class.to_s.split('::').last}-#{self.id}" end |
#blocks_collection ⇒ Object
69 70 71 |
# File 'app/models/contents_core/block.rb', line 69 def blocks_collection self.cc_blocks.map &:as_json end |
#children_type ⇒ Object
73 74 75 |
# File 'app/models/contents_core/block.rb', line 73 def children_type config[:children_type] end |
#config ⇒ Object
217 218 219 |
# File 'app/models/contents_core/block.rb', line 217 def config ContentsCore.config[:cc_blocks][block_type.to_sym] ? ContentsCore.config[:cc_blocks][block_type.to_sym] : {} end |
#create_item(item_type, item_name = nil) ⇒ Object
77 78 79 80 81 82 |
# File 'app/models/contents_core/block.rb', line 77 def create_item( item_type, item_name = nil ) new_item = ContentsCore::Item.new( type: item_type ) new_item.name = item_name if item_name self.items << new_item new_item end |
#editable ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/contents_core/block.rb', line 84 def editable ContentsCore.editing ? ( is_sub_block? ? { 'data-ec-sub-block': self.id, 'data-ec-ct': self.block_type, 'data-ec-position': self.position, 'data-ec-pub': self.published } : { 'data-ec-block': self.id, 'data-ec-container': self.children_type, 'data-ec-ct': self.block_type, 'data-ec-pub': self.published } ).map { |k, v| "#{k}=\"#{v}\"" }.join( ' ' ).html_safe : '' end |
#get(name) ⇒ Object
Returns an item by name
103 104 105 106 |
# File 'app/models/contents_core/block.rb', line 103 def get( name ) item = get_item( name ) item.data if item end |
#get_item(name) ⇒ Object
108 109 110 111 112 113 114 |
# File 'app/models/contents_core/block.rb', line 108 def get_item( name ) unless @_items @_items = {} items.each { |item| @_items[item.name] = item } end @_items[name] end |
#has_children? ⇒ Boolean
120 121 122 |
# File 'app/models/contents_core/block.rb', line 120 def has_children? cc_blocks.exists? end |
#has_parent? ⇒ Boolean
116 117 118 |
# File 'app/models/contents_core/block.rb', line 116 def has_parent? parent.present? end |
#is_sub_block? ⇒ Boolean
124 125 126 |
# File 'app/models/contents_core/block.rb', line 124 def is_sub_block? parent.present? && parent_type == 'ContentsCore::Block' end |
#items_collection ⇒ Object
128 129 130 |
# File 'app/models/contents_core/block.rb', line 128 def items_collection self.items.map &:as_json end |
#on_after_create ⇒ Object
132 133 134 135 |
# File 'app/models/contents_core/block.rb', line 132 def on_after_create # TODO: validates type before creation! Block::init_items( self, config[:items] ) if Block::block_types.include?( self.block_type.to_sym ) end |
#on_before_create ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/models/contents_core/block.rb', line 137 def on_before_create if self.name.blank? names = parent.cc_blocks.map &:name i = 0 while( ( i += 1 ) < 1000 ) # Search an empty group unless names.include? "#{block_type}-#{i}" self.name = "#{block_type}-#{i}" break end end end end |
#props ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/models/contents_core/block.rb', line 150 def props pieces = {} Item::item_types.each do |type| pieces[type.pluralize.to_sym] = [] end items.each do |item| # TODO: improve me pieces[item.class.type_name.pluralize.to_sym].push item end Item::item_types.each do |type| pieces[type.to_sym] = pieces[type.pluralize.to_sym].any? ? pieces[type.pluralize.to_sym].first : nil # EMPTY_DATA - empty Item per sti class? end # pieces = { # images: items.select { |item| item.type == ItemImage.to_s }, # integers: items.select { |item| item.type == ItemInteger.to_s }, # strings: items.select { |item| item.type == ItemString.to_s }, # texts: items.select { |item| item.type == ItemText.to_s }, # } # pieces[:image] = pieces[:images].any? ? pieces[:images].first : EMPTY_DATA # pieces[:integers] = pieces[:integers].any? ? pieces[:integers].first : EMPTY_DATA # pieces[:string] = pieces[:strings].any? ? pieces[:strings].first : EMPTY_DATA # pieces[:text] = pieces[:texts].any? ? pieces[:texts].first : EMPTY_DATA OpenStruct.new( pieces ) end |
#set(name, value) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'app/models/contents_core/block.rb', line 178 def set( name, value ) items.each do |item| if item.name == name item.data = value break end end end |