contents_core 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da496467b39f33f60cba8e1ce3356a83cf26f804
4
- data.tar.gz: a81d0b06211ffaf5c3aa262fd82167c9e88f88ac
3
+ metadata.gz: 3635d8643c427f43b9fb5f929fc98c111bc84994
4
+ data.tar.gz: b5eb85c95998603fac03517e1221dfee6a409b59
5
5
  SHA512:
6
- metadata.gz: 47c86fbecb6193acb88816373765193d7c60cf68ae81ca4ae781675d9b27794ed58b617fb733d32cd70ba7ab3f4d5cd25e106288d5698c23c1a5dcd897c9511e
7
- data.tar.gz: 3dd11bae3f087cf3ad7d6569c6387d5c3aa57240bda029b7cb1b97945974c1979f74de165eed0e8384340b0cb8111c2f6292bd361e1619db3869335be1be4945
6
+ metadata.gz: 0d064f8cdf42dbb1eaed6060c019f2a3c213750bf7fb09a3a835af64b7f52dc1a98dbf9e2e15680a1fd2eeb247f8e53ade283f3e674033d685ca5470ca96e8cb
7
+ data.tar.gz: 1ce8483d568007a1e90b17a725408a1acac654b29b1d2ef0b1605d10ec161d1ef7a2c1cde9623b6e478b1e365f40e5db613949936b90f7dadb20e0a3b295cdf4
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
- # ContentsCore [![Gem Version](https://badge.fury.io/rb/contents_core.svg)](https://badge.fury.io/rb/contents_core)
1
+ # ContentsCore [![Gem Version](https://badge.fury.io/rb/contents_core.svg)](https://badge.fury.io/rb/contents_core) [![Build Status](https://travis-ci.org/blocknotes/contents_core.svg)](https://travis-ci.org/blocknotes/contents_core)
2
2
 
3
3
  A Rails gem which offer a simple structure to manage contents in a flexible way.
4
4
 
5
- Goals:
5
+ Disclaimer: this component is in ALPHA, major changes could happen
6
6
 
7
+ Goals:
7
8
  - attach the contents structure to a model transparently
8
9
  - improve block views management
9
10
  - add fields to blocks without migrations
@@ -108,7 +109,7 @@ If you use ActiveAdmin as admin interface you can find a sample model configurat
108
109
 
109
110
  ### Notes
110
111
 
111
- - Blocks list (enum): `ContentsCore::Block.block_list`
112
+ - Blocks enum: `ContentsCore::Block.block_enum`
112
113
  - Blocks types: `ContentsCore::Block.block_types`
113
114
  - Default blocks [here](config/initializers/contents_core.rb)
114
115
 
@@ -2,6 +2,8 @@ module ContentsCore
2
2
  class Block < ApplicationRecord
3
3
  EMPTY_DATA = OpenStruct.new( { data: '' } )
4
4
 
5
+ attr_accessor :create_children
6
+
5
7
  # --- fields options ----------------------------------------------------- #
6
8
  serialize :options, JSON
7
9
  serialize :validations, JSON
@@ -51,29 +53,29 @@ module ContentsCore
51
53
  #
52
54
  # # scope :published, -> { where( published: true ) unless ApplicationController.edit_mode }
53
55
 
54
- def initialize( params = {} )
55
- super
56
+ def initialize( attributes = {}, &block )
57
+ super( attributes, &block )
58
+ @create_children = 1
56
59
  self.group = config[:group]
57
- self.block_type = parent.config[:children_type] if params[:block_type].nil? && self.parent_type == 'ContentsCore::Block'
60
+ self.block_type = parent.config[:children_type] if attributes[:block_type].nil? && self.parent_type == 'ContentsCore::Block'
58
61
  end
59
62
 
60
- def as_json
61
- # binding.pry
62
- super({ only: [:id, :block_type, :name, :group, :position, :published], methods: [:blocks_collection, :items_collection]}.merge(options || {}))
63
+ def as_json( options = nil )
64
+ super({ only: [:id, :block_type, :name, :group, :position, :published], include: [:cc_blocks, :items]}.merge(options || {}))
63
65
  end
64
66
 
65
67
  def attr_id
66
68
  "#{self.class.to_s.split('::').last}-#{self.id}"
67
69
  end
68
70
 
69
- def blocks_collection
70
- self.cc_blocks.map &:as_json
71
- end
72
-
73
71
  def children_type
74
72
  config[:children_type]
75
73
  end
76
74
 
75
+ def config
76
+ ContentsCore.config[:cc_blocks][block_type.to_sym] ? ContentsCore.config[:cc_blocks][block_type.to_sym] : {}
77
+ end
78
+
77
79
  def create_item( item_type, item_name = nil )
78
80
  new_item = ContentsCore::Item.new( type: item_type )
79
81
  new_item.name = item_name if item_name
@@ -125,18 +127,14 @@ module ContentsCore
125
127
  parent.present? && parent_type == 'ContentsCore::Block'
126
128
  end
127
129
 
128
- def items_collection
129
- self.items.map &:as_json
130
- end
131
-
132
130
  def on_after_create
133
131
  # TODO: validates type before creation!
134
- Block::init_items( self, config[:items] ) if Block::block_types.include?( self.block_type.to_sym )
132
+ Block::init_items( self, config[:items] ) if Block::block_types( false ).include?( self.block_type.to_sym )
135
133
  end
136
134
 
137
135
  def on_before_create
138
136
  if self.name.blank?
139
- names = parent.cc_blocks.map &:name
137
+ names = parent.cc_blocks.map( &:name )
140
138
  i = 0
141
139
  while( ( i += 1 ) < 1000 ) # Search an empty group
142
140
  unless names.include? "#{block_type}-#{i}"
@@ -184,15 +182,15 @@ module ContentsCore
184
182
  end
185
183
  end
186
184
 
187
- def self.block_list
188
- @@block_list ||= ContentsCore.config[:cc_blocks].map{|k, v| [v[:name], k.to_s] unless v[:child_only]}.compact.sort_by{|b| b[0]}
185
+ def self.block_enum( include_children = true )
186
+ ContentsCore.config[:cc_blocks].map{|k, v| [v[:name], k.to_s] if !include_children || !v[:child_only]}.compact.sort_by{|b| b[0]}
189
187
  end
190
188
 
191
- def self.block_types
192
- @@block_types ||= ContentsCore.config[:cc_blocks].keys
189
+ def self.block_types( include_children = true )
190
+ ContentsCore.config[:cc_blocks].select{|k, v| !include_children || !v[:child_only]}.keys
193
191
  end
194
192
 
195
- def self.init_items( block, items )
193
+ def self.init_items( block, items, options = {} )
196
194
  items.each do |name, type|
197
195
  t = type.to_sym
198
196
  if type.to_s.start_with? 'item_'
@@ -204,8 +202,10 @@ module ContentsCore
204
202
  model = false
205
203
  end
206
204
  block.items << model.new( name: name ).init if model
207
- elsif Block::block_types.include? t.to_sym
208
- block.cc_blocks << ( cmp = Block.new( block_type: t, name: name ) )
205
+ elsif Block::block_types( false ).include? t.to_sym
206
+ block.create_children.times do
207
+ block.cc_blocks << Block.new( block_type: t, name: name )
208
+ end
209
209
  end
210
210
  end if items
211
211
  end
@@ -213,9 +213,5 @@ module ContentsCore
213
213
  def self.permitted_attributes
214
214
  [ :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 ] ]
215
215
  end
216
-
217
- def config
218
- ContentsCore.config[:cc_blocks][block_type.to_sym] ? ContentsCore.config[:cc_blocks][block_type.to_sym] : {}
219
- end
220
216
  end
221
217
  end
@@ -6,8 +6,8 @@ module ContentsCore
6
6
 
7
7
  belongs_to :block
8
8
 
9
- def as_json
10
- super( {only: [:id, :name, :type], methods: [:data]} )
9
+ def as_json( options = nil )
10
+ super( {only: [:id, :name, :type], methods: [:data]}.merge(options || {}) )
11
11
  end
12
12
 
13
13
  def attr_id
@@ -32,6 +32,10 @@ module ContentsCore
32
32
  end
33
33
  end
34
34
 
35
+ def process_data( args = nil )
36
+ config[:process_data].call( self.data, args ) if config[:process_data]
37
+ end
38
+
35
39
  def set( value )
36
40
  self.data = value
37
41
  self
@@ -47,7 +51,7 @@ module ContentsCore
47
51
  end
48
52
 
49
53
  def self.item_types
50
- @@item_types ||= ContentsCore.config[:items].keys.map &:to_s
54
+ @@item_types ||= ContentsCore.config[:items].keys.map( &:to_s )
51
55
  end
52
56
 
53
57
  def self.permitted_attributes
@@ -57,7 +61,47 @@ module ContentsCore
57
61
  protected
58
62
 
59
63
  def config
60
- @config ||= self.block.config[:options] && self.block.config[:options][self.name.to_sym] ? self.block.config[:options][self.name.to_sym] : ( ContentsCore.config[:items][self.class::type_name.to_sym] ? ContentsCore.config[:items][self.class::type_name.to_sym] : {} )
64
+ @config ||= self.block && self.block.config[:options] && self.block.config[:options][self.name.to_sym] ? self.block.config[:options][self.name.to_sym] : ( ContentsCore.config[:items][self.class::type_name.to_sym] ? ContentsCore.config[:items][self.class::type_name.to_sym] : {} )
65
+ end
66
+
67
+ def convert_data( value )
68
+ # return ( data = config[:convert_method].call( data ) ) if config[:convert_method]
69
+ if config[:data_type]
70
+ case config[:data_type].to_sym
71
+ when :boolean
72
+ self.data_boolean = ( value == 1 ) || ( value == '1' ) || ( value == 'true' ) || ( value == 'yes' )
73
+ when :float
74
+ self.data_float = value.to_f
75
+ when :integer
76
+ self.data_integer = value.to_i
77
+ when :string
78
+ self.data_string = value.to_s
79
+ when :text
80
+ self.data_text = value.to_s
81
+ else
82
+ return false
83
+ end
84
+ true
85
+ else
86
+ false
87
+ end
88
+ end
89
+
90
+ def converted_data
91
+ if config[:data_type]
92
+ case config[:data_type].to_sym
93
+ when :boolean
94
+ self.data_boolean
95
+ when :float
96
+ self.data_float
97
+ when :integer
98
+ self.data_integer
99
+ when :string
100
+ self.data_string
101
+ when :text
102
+ self.data_text
103
+ end
104
+ end
61
105
  end
62
106
  end
63
107
  end
@@ -1,24 +1,54 @@
1
1
  module ContentsCore
2
2
  class ItemArray < Item
3
- alias_attribute :data, :data_string
4
-
5
3
  serialize :data_hash, Array
4
+ serialize :data_text, Array
5
+
6
+ # after_initialize do
7
+ # config[:data_type] ||= :integer # TODO: this overrides the config !
8
+ # end
9
+
10
+ def data
11
+ is_multiple? ? self.data_text : converted_data
12
+ end
13
+
14
+ def data=( value )
15
+ if is_multiple?
16
+ if config[:data_type]
17
+ self.data_text = case config[:data_type].to_sym
18
+ # when :boolean
19
+ # self.data_boolean = ( value == 1 ) || ( value == '1' ) || ( value == 'true' ) || ( value == 'yes' )
20
+ when :float
21
+ value.map( &:to_f )
22
+ when :integer
23
+ value.map( &:to_i )
24
+ when :string, :text
25
+ value.map( &:to_s )
26
+ else
27
+ value
28
+ end
29
+ else
30
+ self.data_text = value
31
+ end
32
+ else
33
+ convert_data( value )
34
+ end
35
+ end
6
36
 
7
- def enum
8
- config[:values] ? config[:values] : ( config[:values_method] ? config[:values_method].call : self.data_hash )
37
+ def enum( params = nil )
38
+ config[:values] ? config[:values] : ( config[:values_method] ? config[:values_method].call( params ) : self.data_hash )
9
39
  end
10
40
 
11
41
  def init
12
- self.data = ''
42
+ self.data_string = []
13
43
  self
14
44
  end
15
45
 
16
- def to_s
17
- self.data
46
+ def is_multiple?
47
+ config[:multiple] ? true : false
18
48
  end
19
49
 
20
- def self.permitted_attributes
21
- [:data_string, :data_hash]
50
+ def to_s
51
+ self.data
22
52
  end
23
53
 
24
54
  def self.type_name
@@ -8,6 +8,13 @@ en:
8
8
  one: Item
9
9
  other: Items
10
10
  attributes:
11
+ contents_core/block:
12
+ image: Image
13
+ multi_text: Text Only
14
+ slide: Slide
15
+ slider: Slider
16
+ text: Text Only
17
+ text_with_image: Text with Image
11
18
  contents_core/item:
12
19
  column: Column
13
20
  content: Content
@@ -4,7 +4,7 @@ class AddExtraItems < ActiveRecord::Migration[5.0]
4
4
  add_column :contents_core_items, :data_datetime, :datetime
5
5
  add_column :contents_core_items, :data_file, :string # , null: false, default: ''
6
6
  add_column :contents_core_items, :data_float, :float
7
- add_column :contents_core_items, :data_hash, :text # , null: false, default: ''
7
+ add_column :contents_core_items, :data_hash, :text
8
8
  add_column :contents_core_items, :data_integer, :integer
9
9
  add_column :contents_core_items, :data_string, :string # , null: false, default: ''
10
10
  add_column :contents_core_items, :data_text, :text
@@ -12,8 +12,9 @@ module ContentsCore
12
12
  block.name = params[:name] if params[:name]
13
13
  block.options = params[:options] if params[:options]
14
14
  block.validations = params[:validations] if params[:validations]
15
+ block.create_children = params[:create_children].to_i if params[:create_children]
15
16
  parent.cc_blocks << block
16
- Block::init_items block, params[:schema] if params[:schema]
17
+ Block::init_items block, params[:schema], {create_children: params[:create_children]} if params[:schema]
17
18
  block
18
19
  end
19
20
 
@@ -1,3 +1,3 @@
1
1
  module ContentsCore
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contents_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-28 00:00:00.000000000 Z
11
+ date: 2017-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  description: A Rails gem which offer a simple structure to manage contents in a flexible
28
56
  way
29
57
  email: