vimo 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 165c7157085ad7675a72fcb99f22444322f0d235635e58103425a43eaab43da1
4
- data.tar.gz: e54c776796bebdde6113a7d58e77afab59d5a4779dbed48df470c9b912f83707
3
+ metadata.gz: 1005a992604203884f06ec2f60da43f3b990814d981f18fd53e340f68cdfddb9
4
+ data.tar.gz: 17a6dcf1e5b2f2e702d9e107cdf4eacb9e4aa5e421f92fb4874487d92643ccd1
5
5
  SHA512:
6
- metadata.gz: 2ef13ba029adac1b3b17a3ac17c42df29f4ce1a1bc1bb693e0d73ebbc230ab4a805f4ee8f90092d014e963db06dbe8f9f145df6576430a9e6547ba7c1358f86c
7
- data.tar.gz: 8b56cd796a0609b4305a759e93828d1b8fde8eabdf8e8d3b67bc3ed5f5ca0ca022c41d5226ffbfe6e2c0b14e9114b262e41505ad11a79f9a0b19c78606b67f9b
6
+ metadata.gz: 43b32c3e3268e1cdf280cde01ce20c9489b05db23d8ed400ab44a5e68c39b9432437d56a71e74e548d85f4e341605e7d93b61dcd71ba5fbd45e655867b3c09b0
7
+ data.tar.gz: a5ce59a8456d87afa1759455785035568d847e333077a2e56a3d4bad79b02cd5b4143cecc9ed62c857f4fac1b5bb5dceca98b2dabbe6bb636d1166751790d028
data/README.md CHANGED
@@ -5,7 +5,7 @@ Rails engine that allow to your users create and customize their own virtual mod
5
5
  ## Warning
6
6
 
7
7
  This engine is in a very early stage and the DSL and APIs could change, if you
8
- are interesed on use it you can emailme: `ceritium@gmail.com`.
8
+ are interested on use it you can emailme: `ceritium@gmail.com`.
9
9
 
10
10
  ## Installation
11
11
 
@@ -75,7 +75,7 @@ DELETE /vimo/entities/:entity/items/:id
75
75
 
76
76
  Create an item curl example:
77
77
  ```
78
- curl localhost:3000/vimo/entities/comments/items -X POST -H 'Content-Type: application/json' -d '{"title": "a title"}'
78
+ curl localhost:3000/vimo/entities/comments/items -X POST -H 'Content-Type: application/json' -d '{"item" : {"title": "a title"}}'
79
79
  ```
80
80
 
81
81
  Short path version
@@ -107,6 +107,14 @@ end
107
107
 
108
108
  Setting a `owner` scope the uniquenes constraints.
109
109
 
110
+ The owner should respond to `vimo_entities`
111
+
112
+ ```ruby
113
+ class Account < ApplicationRecord
114
+ vimo_owner
115
+ end
116
+ ```
117
+
110
118
 
111
119
  ### Expand your models
112
120
 
@@ -117,15 +125,12 @@ users expand it.
117
125
  class Post < ApplicationRecord
118
126
  belongs_to :account
119
127
 
120
- include Vimo::Expandable # OR add it to ApplicationRecord
121
- expandable owner: :account
128
+ vimo_expand owner: :account
122
129
  end
123
130
  ```
124
131
 
125
- Now can manage the virtual attributes of the model `Post` with the identifier `_extend_posts`.
126
-
132
+ Now can manage the virtual attributes of the model `Post` with the identifier `_expand_posts`.
127
133
 
128
- WIP
129
134
 
130
135
  ## Contributing
131
136
  Contribution directions go here.
@@ -34,7 +34,7 @@ module Vimo
34
34
  private
35
35
 
36
36
  def save_and_respond
37
- @item.assign_params(params)
37
+ @item.data = params[:item]
38
38
  @item.save
39
39
 
40
40
  respond_with @entity, @item
@@ -7,6 +7,9 @@ module Vimo
7
7
  belongs_to :expandable, polymorphic: true, required: false
8
8
 
9
9
  validate :entity_definition
10
+ validate :expanded
11
+ validates :expandable_id, uniqueness: { scope: :expandable_type },
12
+ allow_nil: true
10
13
 
11
14
  class << self
12
15
  def parse(value_o, kind)
@@ -43,26 +46,21 @@ module Vimo
43
46
  ({ id: id }).merge(data)
44
47
  end
45
48
 
46
- def assign_params(params)
47
- self.data ||= {}
48
- self.data = entity.fields.inject(self.data) do |memo, field|
49
-
50
- name = field.name.to_s
51
- kind = field.kind.to_s
49
+ private
52
50
 
53
- att = params[name]
54
- if params.keys.include?(name)
55
- memo[name] = self.class.parse(att, kind)
51
+ def expanded
52
+ if entity.expand_model.present?
53
+ if expandable.present?
54
+ unless expandable.model_name.name == entity.expand_model
55
+ errors.add(:expandable, "is invalid")
56
+ end
57
+ else
58
+ errors.add(:expandable, "is required")
56
59
  end
57
-
58
- memo
59
60
  end
60
61
  end
61
62
 
62
- private
63
-
64
63
  def entity_definition
65
- self.data ||= {}
66
64
  entity.fields.each do |field|
67
65
  name = field.name.to_s
68
66
  if field.required
@@ -4,6 +4,7 @@ class CreateVimoEntities < ActiveRecord::Migration[5.0]
4
4
  t.string :name
5
5
  t.string :system_name
6
6
  t.references :owner, polymorphic: true, index: true
7
+ t.string :expand_model
7
8
 
8
9
  t.timestamps
9
10
  end
@@ -2,7 +2,7 @@ class CreateVimoItems < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  create_table :vimo_items do |t|
4
4
  t.references :entity, foreign_key: true
5
- t.references :expandable, polymorphic: true, index: true
5
+ t.references :expandable, polymorphic: true, index: { unique: true }
6
6
  t.text :data
7
7
 
8
8
  t.timestamps
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "vimo/engine"
4
4
  require "vimo/configuration"
5
+ require "vimo/ownerable"
6
+ require "vimo/expandable"
5
7
 
6
8
  module Vimo
7
9
  # Your code goes here...
@@ -7,15 +7,15 @@ module Vimo
7
7
  included do
8
8
 
9
9
  def self.vimo_expand(options = {})
10
- @@belongs_to_owner = options[:owner]
10
+ @belongs_to_owner = options[:owner]
11
+ data_method = options[:data_method] || "data"
11
12
 
12
13
  self.class_eval do
13
-
14
- has_one :expanded, as: :expandable, class_name: "Vimo::Item"
15
- delegate :data, to: :expanded
14
+ has_one :expanded, as: :expandable, class_name: "Vimo::Item", autosave: true
15
+ delegate data_method, "#{data_method}=", to: :expanded
16
16
 
17
17
  def vimo_owner
18
- send(@@belongs_to_owner) if @@belongs_to_owner
18
+ send(@belongs_to_owner) if @belongs_to_owner
19
19
  end
20
20
 
21
21
  def entities
@@ -27,7 +27,7 @@ module Vimo
27
27
  end
28
28
 
29
29
  def expanded
30
- super || build_expanded(entity: entities.find_or_create_by!(name: "_expand_#{self.class.table_name}"))
30
+ super || build_expanded(entity: entities.find_or_create_by!(name: "_expand_#{self.class.table_name}", expand_model: self.model_name.name))
31
31
  end
32
32
 
33
33
  def entity_fields
@@ -38,3 +38,5 @@ module Vimo
38
38
  end
39
39
  end
40
40
  end
41
+
42
+ ActiveRecord::Base.send :include, Vimo::Expandable
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vimo
4
+ module Ownerable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def self.vimo_owner(options = {})
9
+ self.class_eval do
10
+ has_many :vimo_entities, class_name: "Vimo::Entity", as: :owner
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ ActiveRecord::Base.send :include, Vimo::Ownerable
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vimo
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vimo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jose Galisteo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-18 00:00:00.000000000 Z
11
+ date: 2018-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kaminari
@@ -149,7 +149,6 @@ files:
149
149
  - app/mailers/vimo/application_mailer.rb
150
150
  - app/models/vimo/application_record.rb
151
151
  - app/models/vimo/entity.rb
152
- - app/models/vimo/expandable.rb
153
152
  - app/models/vimo/field.rb
154
153
  - app/models/vimo/item.rb
155
154
  - app/models/vimo/system_name.rb
@@ -166,6 +165,8 @@ files:
166
165
  - lib/vimo.rb
167
166
  - lib/vimo/configuration.rb
168
167
  - lib/vimo/engine.rb
168
+ - lib/vimo/expandable.rb
169
+ - lib/vimo/ownerable.rb
169
170
  - lib/vimo/version.rb
170
171
  homepage: https://github.com/ceritium/vimo
171
172
  licenses: