contents_core 0.1.9 → 0.2.0

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: be0478a45deafccaf2eb6aebee455e6af25a8c82
4
- data.tar.gz: 8de680af051e77b3396be8f79ba3c059d66b3b71
3
+ metadata.gz: 638db7f2736f8122e4d3df7fd00c14be26b5968b
4
+ data.tar.gz: 2939dfe17490c73f03a9ca541c15cfb9959a5726
5
5
  SHA512:
6
- metadata.gz: 560a8da1c70ddca4ce964d67554f6889a09a87dee1e1b46fabd04fa6ddd9d825f2862af28a3a0c40ca2bf712da22d7ef91d7c23720b441f1ba7161a36008ec71
7
- data.tar.gz: 8d9d89e770ff43184d85dcf5a7ac7aa41243d70643a3a910bcd2563cb213f60423f0f5fe5d1427d683182009c21eff292c85f7667e4cd7fb545cb560f1eeb43b
6
+ metadata.gz: dc24cccc25294ddaaf58900d936ecaab4b7ffb3e91c212daa96259b329699109180f8b1c816f5df46594363be3fa91a407b2f07dd904407b6487aaac80177ae0
7
+ data.tar.gz: 12fdb14837f0275ed3cdf34abc74087fb615c6439975f4b2fa9713470480ae6c61a786c28701c94399af22c7e5be6238acf923182d2ca3beb7a6803b459d0be6
data/README.md CHANGED
@@ -2,28 +2,20 @@
2
2
 
3
3
  A Rails gem which offer a simple structure to manage contents in a flexible way.
4
4
 
5
- _NOTE_: this is an **ALPHA** version, major changes could happens - this is a refactoring of another gem of mine (editable_components)
6
-
7
5
  Goals:
8
6
 
9
7
  - attach the contents structure to a model transparently
10
-
11
8
  - improve block views management
12
-
13
9
  - add fields to blocks without migrations
14
10
 
15
11
  ### Install
16
12
 
17
13
  - Add to the Gemfile:
18
14
  `gem 'contents_core'`
19
-
20
15
  - Copy migrations (Rails 5.x syntax, in Rails 4.x use `rake`):
21
16
  `rails contents_core:install:migrations`
22
-
23
17
  - Execute migrations
24
-
25
18
  - Add the concern *Blocks* to your model (ex. *Page*): `include ContentsCore::Blocks`
26
-
27
19
  - Add the blocks to a view (ex. *page show*): `= render partial: 'contents_core/blocks', locals: { container: @page }`
28
20
 
29
21
  ### Config
@@ -116,16 +108,14 @@ If you use ActiveAdmin as admin interface you can find a sample model configurat
116
108
 
117
109
  ### Notes
118
110
 
111
+ - Blocks list (enum): `ContentsCore::Block.block_list`
119
112
  - Blocks types: `ContentsCore::Block.block_types`
120
-
121
113
  - Default blocks [here](config/initializers/contents_core.rb)
122
114
 
123
115
  #### Structure
124
116
 
125
117
  - Including the Blocks concern to a model will add `has_many :cc_blocks` relationship (the list of blocks attached to a container) and some utility methods
126
-
127
118
  - Block: UI component, a group of items (ex. a text with a title, a slider, a 3 column text widget, etc.); built with a list of sub blocks (for nested components) and a list of items
128
-
129
119
  - Item: a single piece of information (ex. a string, a text, a boolean, an integer, a file, etc.) with a virtual method named *data*
130
120
 
131
121
  ## Contributors
@@ -56,7 +56,7 @@ module ContentsCore
56
56
  end
57
57
 
58
58
  def children_type
59
- ContentsCore.config[:cc_blocks][block_type.to_sym][:children_type]
59
+ config[:children_type]
60
60
  end
61
61
 
62
62
  def create_item( item_type, item_name = nil )
@@ -107,8 +107,7 @@ module ContentsCore
107
107
 
108
108
  def on_after_create
109
109
  # TODO: validates type before creation!
110
- t = self.block_type.to_sym
111
- Block::init_items( self, ContentsCore.config[:cc_blocks][t][:items] ) if Block::block_types.include?( t )
110
+ Block::init_items( self, config[:items] ) if Block::block_types.include?( self.block_type.to_sym )
112
111
  end
113
112
 
114
113
  def on_before_create
@@ -161,6 +160,10 @@ module ContentsCore
161
160
  end
162
161
  end
163
162
 
163
+ def self.block_list
164
+ @@block_list ||= ContentsCore.config[:cc_blocks].map{|k, v| [v[:name], k.to_s]}.sort_by{|b| b[0]}
165
+ end
166
+
164
167
  def self.block_types
165
168
  @@block_types ||= ContentsCore.config[:cc_blocks].keys
166
169
  end
@@ -186,5 +189,9 @@ module ContentsCore
186
189
  def self.permitted_attributes
187
190
  [ :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 ] ]
188
191
  end
192
+
193
+ def config
194
+ @config ||= ContentsCore.config[:cc_blocks][block_type.to_sym] ? ContentsCore.config[:cc_blocks][block_type.to_sym] : {}
195
+ end
189
196
  end
190
197
  end
@@ -53,7 +53,7 @@ module ContentsCore
53
53
  protected
54
54
 
55
55
  def config
56
- @config ||= ContentsCore.config[:items][self.class::type_name.to_sym] ? ContentsCore.config[:items][self.class::type_name.to_sym] : {}
56
+ @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] : {} )
57
57
  end
58
58
  end
59
59
  end
@@ -0,0 +1,28 @@
1
+ module ContentsCore
2
+ class ItemEnum < Item
3
+ alias_attribute :data, :data_string
4
+
5
+ serialize :data_hash, Array
6
+
7
+ def enum
8
+ config[:values] ? config[:values] : self.data_hash
9
+ end
10
+
11
+ def init
12
+ self.data = ''
13
+ self
14
+ end
15
+
16
+ def to_s
17
+ self.data
18
+ end
19
+
20
+ def self.permitted_attributes
21
+ [:data_string, :data_hash]
22
+ end
23
+
24
+ def self.type_name
25
+ 'enum'
26
+ end
27
+ end
28
+ end
@@ -2,13 +2,17 @@ module ContentsCore
2
2
  class ItemHash < Item
3
3
  alias_attribute :data, :data_hash
4
4
 
5
- serialize :data_hash, JSON
5
+ serialize :data_hash, Hash
6
6
 
7
7
  def init
8
8
  self.data = {}
9
9
  self
10
10
  end
11
11
 
12
+ def keys
13
+ config[:keys] ? config[:keys] : self.data_hash.keys
14
+ end
15
+
12
16
  def from_string( value )
13
17
  if value.is_a? String
14
18
  val = {}
@@ -20,8 +24,17 @@ module ContentsCore
20
24
  end
21
25
  end
22
26
 
27
+ def method_missing( method, *args, &block )
28
+ matches = /data_(.+)=/.match method.to_s
29
+ self.data[matches[1]] = args[0] if matches[1]
30
+ end
31
+
32
+ def respond_to?( method, include_private = false )
33
+ method.to_s.starts_with?( 'data_' ) || super
34
+ end
35
+
23
36
  def to_s
24
- self.data_hash.inject( '' ) { |k, v| k + v[0] + ': ' + v[1] + "\n" }
37
+ self.data_hash ? self.data_hash.inject( '' ) { |k, v| k + v[0] + ': ' + v[1] + "\n" } : {}
25
38
  end
26
39
 
27
40
  def self.permitted_attributes
@@ -0,0 +1,36 @@
1
+ # TODO: needs improvements
2
+ module ContentsCore
3
+ class ItemObject < Item
4
+ alias_attribute :data, :data_hash
5
+
6
+ serialize :data_hash, JSON
7
+
8
+ def init
9
+ self.data = {}
10
+ self
11
+ end
12
+
13
+ def from_string( value )
14
+ if value.is_a? String
15
+ val = {}
16
+ value.each_line do |line|
17
+ m = line.match( /([^:]*):(.*)/ )
18
+ val[m[1]] = m[2].strip if m && !m[1].blank?
19
+ end
20
+ self.data_hash = val
21
+ end
22
+ end
23
+
24
+ def to_s
25
+ self.data_hash ? self.data_hash.inject( '' ) { |k, v| k + v[0] + ': ' + v[1] + "\n" } : {}
26
+ end
27
+
28
+ def self.permitted_attributes
29
+ [ :data_hash ]
30
+ end
31
+
32
+ def self.type_name
33
+ 'object'
34
+ end
35
+ end
36
+ end
@@ -49,12 +49,14 @@ module ContentsCore
49
49
  items: {
50
50
  boolean: {},
51
51
  datetime: {},
52
+ enum: {},
52
53
  float: {},
53
54
  hash: {},
54
55
  file: {
55
56
  input: :file_image
56
57
  },
57
58
  integer: {},
59
+ object: {},
58
60
  string: {},
59
61
  text: {
60
62
  input: :html
@@ -1,15 +1,16 @@
1
1
  class CreateContentsCoreBlocks < ActiveRecord::Migration[5.0]
2
2
  def change
3
3
  create_table :contents_core_blocks do |t|
4
- t.string :block_type, null: false, default: 'text'
4
+ t.string :block_type, null: false, default: 'text'
5
5
  t.integer :version, null: false, default: 0
6
- t.string :name, null: false, default: ''
6
+ t.string :name, null: false, default: ''
7
+ t.string :group
7
8
  t.integer :position, null: false, default: 0
8
9
  t.boolean :published, null: false, default: true
9
- t.string :options, null: false, default: '{}'
10
- t.string :validations, null: false, default: '{}'
10
+ t.string :options, null: false, default: '{}'
11
+ t.string :validations, null: false, default: '{}'
11
12
  t.integer :parent_id
12
- t.string :parent_type
13
+ t.string :parent_type
13
14
  end
14
15
 
15
16
  add_index :contents_core_blocks, [:parent_id, :parent_type]
@@ -1,8 +1,8 @@
1
1
  class CreateContentsCoreItems < ActiveRecord::Migration[5.0]
2
2
  def change
3
3
  create_table :contents_core_items do |t|
4
- t.string :type
5
- t.string :name, null: false, default: 'data'
4
+ t.string :type
5
+ t.string :name, null: false, default: 'data'
6
6
  t.integer :block_id
7
7
  end
8
8
 
@@ -1,3 +1,3 @@
1
1
  module ContentsCore
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
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.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-16 00:00:00.000000000 Z
11
+ date: 2017-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -40,10 +40,12 @@ files:
40
40
  - app/models/contents_core/item.rb
41
41
  - app/models/contents_core/item_boolean.rb
42
42
  - app/models/contents_core/item_datetime.rb
43
+ - app/models/contents_core/item_enum.rb
43
44
  - app/models/contents_core/item_file.rb
44
45
  - app/models/contents_core/item_float.rb
45
46
  - app/models/contents_core/item_hash.rb
46
47
  - app/models/contents_core/item_integer.rb
48
+ - app/models/contents_core/item_object.rb
47
49
  - app/models/contents_core/item_string.rb
48
50
  - app/models/contents_core/item_text.rb
49
51
  - app/views/contents_core/_block.html.erb
@@ -85,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
87
  version: '0'
86
88
  requirements: []
87
89
  rubyforge_project:
88
- rubygems_version: 2.5.2
90
+ rubygems_version: 2.6.13
89
91
  signing_key:
90
92
  specification_version: 4
91
93
  summary: Flexible contents structure for Rails