Class: EditableComponents::Block
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- EditableComponents::Block
- Defined in:
- app/models/editable_components/block.rb
Constant Summary collapse
- EMPTY_DATA =
OpenStruct.new( { data: '' } )
Class Method Summary collapse
Instance Method Summary collapse
-
#attr_id ⇒ Object
after_validation :on_after_validation.
- #children_type ⇒ Object
- #editable ⇒ Object
-
#get(name) ⇒ Object
Returns an item by name.
- #has_children? ⇒ Boolean
- #has_parent? ⇒ Boolean
- #init ⇒ Object
- #init_items(block, items) ⇒ Object
- #is_sub_block? ⇒ Boolean
- #on_before_create ⇒ Object
- #props ⇒ Object
Class Method Details
.block_types ⇒ Object
172 173 174 |
# File 'app/models/editable_components/block.rb', line 172 def self.block_types @@block_types ||= EditableComponents.config[:ec_blocks].keys end |
Instance Method Details
#attr_id ⇒ Object
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 :ec_blocks, cascade_callbacks: true, order: :position.desc, class_name: ‘EditableComponents::Block’ embeds_many :items, cascade_callbacks: true, class_name: ‘EditableComponents::Item’
accepts_nested_attributes_for :ec_blocks, allow_destroy: true accepts_nested_attributes_for :items
# scope :published, -> { where( published: true ) unless ApplicationController.edit_mode }
53 54 55 |
# File 'app/models/editable_components/block.rb', line 53 def attr_id "#{self.class.to_s.split('::').last}-#{self.id}" end |
#children_type ⇒ Object
57 58 59 |
# File 'app/models/editable_components/block.rb', line 57 def children_type EditableComponents.config[:ec_blocks][block_type.to_sym][:children_type] end |
#editable ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/editable_components/block.rb', line 61 def editable Engine.edit_mode ? ( 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
80 81 82 83 84 85 86 |
# File 'app/models/editable_components/block.rb', line 80 def get( name ) unless @_items @_items = {} items.each { |item| @_items[item.name] = item } end @_items[name] end |
#has_children? ⇒ Boolean
119 120 121 |
# File 'app/models/editable_components/block.rb', line 119 def has_children? ec_blocks.exists? end |
#has_parent? ⇒ Boolean
115 116 117 |
# File 'app/models/editable_components/block.rb', line 115 def has_parent? parent.present? end |
#init ⇒ Object
88 89 90 91 92 93 |
# File 'app/models/editable_components/block.rb', line 88 def init t = block_type.to_sym if Block::block_types.include? t init_items self, EditableComponents.config[:ec_blocks][t][:items] end end |
#init_items(block, items) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/editable_components/block.rb', line 95 def init_items( block, items ) items.each do |name, type| t = type.to_sym if type.to_s.start_with? 'item_' c = 'EditableComponents::' + ActiveSupport::Inflector.camelize( t ) begin model = c.constantize rescue Exception => e Rails.logger.error '[ERROR] EditableComponents - init_items: ' + e. model = false end block.items << model.new( name: name ).init if model elsif Block::block_types.include? t.to_sym cmp = Block.new( block_type: t, name: name ) block.ec_blocks << cmp init_items( cmp, EditableComponents.config[:ec_blocks][t][:items] ) end end if items end |
#is_sub_block? ⇒ Boolean
123 124 125 |
# File 'app/models/editable_components/block.rb', line 123 def is_sub_block? parent.present? && parent_type == 'EditableComponents::Block' end |
#on_before_create ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/editable_components/block.rb', line 127 def on_before_create if self._init self._init = false if self.name.blank? names = parent.ec_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 init end end |
#props ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/models/editable_components/block.rb', line 144 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 |