databound 0.0.3 → 0.1.0

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
  SHA1:
3
- metadata.gz: 22275b2aa2e135427ffb2cccc53091c913706ef5
4
- data.tar.gz: 93ad22a8162a45950b0bee675b03a098c6fa663c
3
+ metadata.gz: 289b15c39df408fc4735e260935f31f1ba838864
4
+ data.tar.gz: cce0136f4944ad481260a3f67f87f89cd0ccb0ea
5
5
  SHA512:
6
- metadata.gz: 1e9ae816ef58f24a27a57cdc819fcaea49b250ae8bf9feffca6c4fa7b998c1f1629863b044a6f27bf60bff641c9df200f7b372a72f3529acb20483e7f45be6be
7
- data.tar.gz: 27dbd28d0751ba595b92836eb2c271a7a45a8b127cf31a04de620dc79c08044485635713824fe373dc0cdc123657e07b68a3653d5fb2de09671d96f87f978398
6
+ metadata.gz: 54c8cce84add784169e125791bf03f0539d1f12811ad3aef0f449062cb2276af591a22910aac8d456526864ca9041ae009ee72f582f2b95fc4d1bd9976fc59ce
7
+ data.tar.gz: 20094e491ebdd45925315c42bc1d90c9f28e376663b36c864f726363a426c81930749dbf3e5bf3905a2885366903cfdf7e68c74587c92f529016f252f40db286
data/README.md CHANGED
@@ -1,19 +1,19 @@
1
- [![Code Climate](https://codeclimate.com/github/Nedomas/databound/badges/gpa.svg)](https://codeclimate.com/github/Nedomas/databound-rails)
1
+ [![Code Climate](https://codeclimate.com/github/Nedomas/databound-rails/badges/gpa.svg)](https://codeclimate.com/github/Nedomas/databound-rails)
2
2
  [![Gem Version](https://badge.fury.io/rb/databound.svg)](http://badge.fury.io/rb/databound)
3
- [![Build Status](https://travis-ci.org/Nedomas/databound.svg?branch=master)](https://travis-ci.org/Nedomas/databound-rails)
4
- [![Dependency Status](https://gemnasium.com/Nedomas/databound.svg)](https://gemnasium.com/Nedomas/databound-rails)
3
+ [![Build Status](https://travis-ci.org/Nedomas/databound-rails.svg?branch=master)](https://travis-ci.org/Nedomas/databound-rails)
4
+ [![Dependency Status](https://gemnasium.com/Nedomas/databound-rails.svg)](https://gemnasium.com/Nedomas/databound-rails)
5
5
 
6
6
  ![Databound](https://cloud.githubusercontent.com/assets/1877286/4743542/df89dcec-5a28-11e4-9114-6f383fe269cb.png)
7
7
 
8
- Exposes ActiveRecord records to the Javascript side.
8
+ Exposes database ORM to the Javascript side.
9
9
 
10
- This is the **Ruby on Rails** backend part for the ``Databound`` javascript lib.
10
+ Ruby on Rails backend for the Databound javascript lib. Supports ActiveRecord and Mongoid
11
11
 
12
12
  For more information go to [javascript Databound repo](https://github.com/Nedomas/databound).
13
13
 
14
14
  ## Javascript library
15
15
 
16
- It does something like this out of the box.
16
+ Out of the box it does something like this.
17
17
 
18
18
  ```js
19
19
  User = new Databound('/users');
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Databound::VERSION
9
9
  spec.authors = ['Domas Bitvinskas']
10
10
  spec.email = ['domas.bitvinskas@me.com']
11
- spec.summary = %q{ActiveRecord exposed to the Javascript side and guarded by guns}
12
- spec.description = %q{This is the Ruby on Rails backend part for the Databound javascript lib.}
11
+ spec.summary = %q{Exposes database ORM to the Javascript side}
12
+ spec.description = %q{Ruby on Rails backend for the Databound javascript lib. Supports ActiveRecord and Mongoid}
13
13
  spec.homepage = 'https://github.com/Nedomas/databound'
14
14
  spec.license = 'MIT'
15
15
 
@@ -18,12 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_dependency 'andand'
22
21
  spec.add_development_dependency 'rspec-rails'
23
22
  spec.add_development_dependency 'combustion', '~> 0.5.2'
24
23
  spec.add_development_dependency 'rails'
25
24
  spec.add_development_dependency 'sqlite3'
26
-
27
25
  spec.add_development_dependency 'pry'
28
26
  spec.add_development_dependency 'pry-stack_explorer'
29
27
  spec.add_development_dependency 'bundler', '~> 1.6'
@@ -1,5 +1,4 @@
1
- require 'andand'
2
-
1
+ require 'databound/extensions'
3
2
  require 'databound/version'
4
3
  require 'databound/data'
5
4
  require 'databound/manager'
@@ -13,8 +12,7 @@ module Databound
13
12
 
14
13
  def where
15
14
  records = @crud.find_scoped_records
16
-
17
- render json: serialized(records)
15
+ render json: serialize_array(records)
18
16
  end
19
17
 
20
18
  def create
@@ -22,7 +20,8 @@ module Databound
22
20
 
23
21
  render json: {
24
22
  success: true,
25
- id: record.id,
23
+ id: serialize(record, :id),
24
+ scoped_records: serialize_array(scoped_records),
26
25
  }
27
26
  end
28
27
 
@@ -31,7 +30,8 @@ module Databound
31
30
 
32
31
  render json: {
33
32
  success: true,
34
- id: record.id,
33
+ id: serialize(record, :id),
34
+ scoped_records: serialize_array(scoped_records),
35
35
  }
36
36
  end
37
37
 
@@ -40,12 +40,13 @@ module Databound
40
40
 
41
41
  render json: {
42
42
  success: true,
43
+ scoped_records: serialize_array(scoped_records),
43
44
  }
44
45
  end
45
46
 
46
47
  private
47
48
 
48
- def serialized(records)
49
+ def serialize_array(records)
49
50
  return records unless defined?(ActiveModel::Serializer)
50
51
 
51
52
  serializer = ActiveModel::Serializer.serializer_for(records.first)
@@ -54,19 +55,47 @@ module Databound
54
55
  ActiveModel::ArraySerializer.new(records).to_json
55
56
  end
56
57
 
58
+ def serialize(record, attribute)
59
+ unserialized = record.send(attribute)
60
+ return unserialized unless defined?(ActiveModel::Serializer)
61
+
62
+ serializer = ActiveModel::Serializer.serializer_for(record)
63
+ return unserialized unless serializer
64
+
65
+ serializer.new(record).attributes[:id]
66
+ end
67
+
57
68
  def model
58
69
  raise 'Override model method to specify a model to be used in CRUD'
59
70
  end
60
71
 
61
72
  def permitted_columns
62
73
  # permit all by default
63
- model.column_names
74
+ if mongoid?
75
+ model.fields.keys.map(&:to_sym)
76
+ elsif activerecord?
77
+ model.column_names
78
+ else
79
+ raise 'ORM not supported. Use ActiveRecord or Mongoid'
80
+ end
81
+ end
82
+
83
+ def mongoid?
84
+ defined?(Moigoid) and model.ancestors.include?(Mongoid::Document)
85
+ end
86
+
87
+ def activerecord?
88
+ defined?(ActiveRecord) and model.ancestors.include?(ActiveRecord::Base)
64
89
  end
65
90
 
66
91
  def init_crud
67
92
  @crud = Databound::Manager.new(self)
68
93
  end
69
94
 
95
+ def scoped_records
96
+ @crud.find_scoped_records(only_extra_scopes: true)
97
+ end
98
+
70
99
  module ClassMethods
71
100
  attr_reader :dsls
72
101
  attr_reader :stricts
@@ -24,16 +24,16 @@ module Databound
24
24
  check_strict!(key, val)
25
25
 
26
26
  block = dsl_block(key, val)
27
- obj[key] = block ? block.call(@params.to_options) : val
27
+ obj[key] = block ? @controller.instance_exec(@params.to_options, &block) : val
28
28
  end
29
29
  end
30
30
 
31
31
  def dsl_block(key, val)
32
- dsl_key(key).andand[val]
32
+ swallow_nil { dsl_key(key)[val] }
33
33
  end
34
34
 
35
35
  def dsl_key(key)
36
- @controller.class.dsls.andand[key]
36
+ swallow_nil { @controller.class.dsls[key] }
37
37
  end
38
38
 
39
39
  def check_strict!(key, val)
@@ -45,7 +45,7 @@ module Databound
45
45
  end
46
46
 
47
47
  def strict?(key)
48
- @controller.class.stricts.andand[key]
48
+ swallow_nil { @controller.class.stricts[key] }
49
49
  end
50
50
  end
51
51
  end
@@ -0,0 +1,7 @@
1
+ # module Databound
2
+ # end
3
+ def swallow_nil
4
+ yield
5
+ rescue NoMethodError
6
+ nil
7
+ end
@@ -17,7 +17,7 @@ module Databound
17
17
  end
18
18
  end
19
19
 
20
- def find_scoped_records
20
+ def find_scoped_records(only_extra_scopes: false)
21
21
  records = []
22
22
  records << @scope.records(@model)
23
23
 
@@ -25,7 +25,11 @@ module Databound
25
25
  records << extra_scope.records(@model)
26
26
  end
27
27
 
28
- records.map { |record| record.where(@data) }.flatten
28
+ if only_extra_scopes
29
+ records.flatten
30
+ else
31
+ records.map { |record| record.where(@data) }.flatten
32
+ end
29
33
  end
30
34
 
31
35
  def create_from_data
@@ -50,11 +54,15 @@ module Databound
50
54
  private
51
55
 
52
56
  def check_params!
53
- requested = [@scope, @data].map(&:to_h).flat_map(&:keys)
54
- unpermitted = requested - @permitted_columns.map(&:to_s)
55
- return if unpermitted.empty?
57
+ return if @permitted_columns == :all
58
+ return if unpermitted_columns.empty?
56
59
 
57
- raise NotPermittedError, "Request includes unpermitted columns: #{unpermitted.join(', ')}"
60
+ raise NotPermittedError, "Request includes unpermitted columns: #{unpermitted_columns.join(', ')}"
61
+ end
62
+
63
+ def unpermitted_columns
64
+ requested = [@scope, @data].map(&:to_h).flat_map(&:keys)
65
+ requested - @permitted_columns.map(&:to_s)
58
66
  end
59
67
  end
60
68
  end
@@ -1,3 +1,3 @@
1
1
  module Databound
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -15,7 +15,11 @@ describe UsersController, type: :controller do
15
15
  end
16
16
 
17
17
  it 'responds consistently to js' do
18
- expect(rubize(response)).to eq(success: true, id: 1)
18
+ expect(rubize(response)).to eq(
19
+ success: true,
20
+ id: 1,
21
+ scoped_records: all_records,
22
+ )
19
23
  end
20
24
 
21
25
  it 'creates the record' do
@@ -79,7 +83,11 @@ describe UsersController, type: :controller do
79
83
  end
80
84
 
81
85
  it 'respond with updated record id' do
82
- expect(rubize(response)).to eq(success: true, id: @user.id)
86
+ expect(rubize(response)).to eq(
87
+ success: true,
88
+ id: @user.id,
89
+ scoped_records: all_records,
90
+ )
83
91
  end
84
92
 
85
93
  it 'do the update' do
@@ -119,7 +127,10 @@ describe UsersController, type: :controller do
119
127
  end
120
128
 
121
129
  it 'respond with success' do
122
- expect(rubize(response)).to eq(success: true)
130
+ expect(rubize(response)).to eq(
131
+ success: true,
132
+ scoped_records: all_records,
133
+ )
123
134
  end
124
135
  end
125
136
 
@@ -18,7 +18,11 @@ describe DslController, type: :controller do
18
18
  end
19
19
 
20
20
  it 'responds consistently to js' do
21
- expect(rubize(response)).to eq(success: true, id: 1)
21
+ expect(rubize(response)).to eq(
22
+ success: true,
23
+ id: 1,
24
+ scoped_records: all_records,
25
+ )
22
26
  end
23
27
 
24
28
  it 'creates the record' do
@@ -48,7 +52,11 @@ describe DslController, type: :controller do
48
52
  end
49
53
 
50
54
  it 'responds consistently to js' do
51
- expect(rubize(response)).to eq(success: true, id: 1)
55
+ expect(rubize(response)).to eq(
56
+ success: true,
57
+ id: 1,
58
+ scoped_records: all_records,
59
+ )
52
60
  end
53
61
 
54
62
  it 'creates the record' do
@@ -103,7 +111,11 @@ describe DslController, type: :controller do
103
111
  end
104
112
 
105
113
  it 'responds consistently to js' do
106
- expect(rubize(response)).to eq(success: true, id: 1)
114
+ expect(rubize(response)).to eq(
115
+ success: true,
116
+ id: 1,
117
+ scoped_records: all_records,
118
+ )
107
119
  end
108
120
 
109
121
  it 'updates the record' do
@@ -136,7 +148,11 @@ describe DslController, type: :controller do
136
148
  end
137
149
 
138
150
  it 'responds consistently to js' do
139
- expect(rubize(response)).to eq(success: true, id: 1)
151
+ expect(rubize(response)).to eq(
152
+ success: true,
153
+ id: 1,
154
+ scoped_records: all_records,
155
+ )
140
156
  end
141
157
 
142
158
  it 'updates the record' do
@@ -16,7 +16,11 @@ describe LooseDslController, type: :controller do
16
16
  end
17
17
 
18
18
  it 'responds consistently to js' do
19
- expect(rubize(response)).to eq(success: true, id: 1)
19
+ expect(rubize(response)).to eq(
20
+ success: true,
21
+ id: 1,
22
+ scoped_records: all_records,
23
+ )
20
24
  end
21
25
 
22
26
  it 'creates the record' do
@@ -46,7 +50,11 @@ describe LooseDslController, type: :controller do
46
50
  end
47
51
 
48
52
  it 'responds consistently to js' do
49
- expect(rubize(response)).to eq(success: true, id: 1)
53
+ expect(rubize(response)).to eq(
54
+ success: true,
55
+ id: 1,
56
+ scoped_records: all_records,
57
+ )
50
58
  end
51
59
 
52
60
  it 'creates the record' do
@@ -78,7 +86,11 @@ describe LooseDslController, type: :controller do
78
86
  end
79
87
 
80
88
  it 'responds consistently to js' do
81
- expect(rubize(response)).to eq(success: true, id: 1)
89
+ expect(rubize(response)).to eq(
90
+ success: true,
91
+ id: 1,
92
+ scoped_records: all_records,
93
+ )
82
94
  end
83
95
 
84
96
  it 'updates the record' do
@@ -110,7 +122,11 @@ describe LooseDslController, type: :controller do
110
122
  end
111
123
 
112
124
  it 'responds consistently to js' do
113
- expect(rubize(response)).to eq(success: true, id: 1)
125
+ expect(rubize(response)).to eq(
126
+ success: true,
127
+ id: 1,
128
+ scoped_records: all_records,
129
+ )
114
130
  end
115
131
 
116
132
  it 'updates the record' do
@@ -19,7 +19,7 @@ def javascriptize(ruby_obj)
19
19
  end
20
20
 
21
21
  def rubize(response)
22
- optionize(JSON.parse(response.body))
22
+ convert_scoped_records(optionize(JSON.parse(response.body)))
23
23
  end
24
24
 
25
25
  def optionize(obj)
@@ -31,6 +31,22 @@ def optionize(obj)
31
31
  end
32
32
  end
33
33
 
34
+ def convert_scoped_records(obj)
35
+ return obj unless obj.is_a?(Hash)
36
+ result = obj
37
+
38
+ converted = obj[:scoped_records].map do |record|
39
+ record.except('created_at', 'updated_at')
40
+ end
41
+
42
+ result[:scoped_records] = converted
43
+ result
44
+ end
45
+
34
46
  def gather(attribute, response)
35
47
  rubize(response).map { |record| record[attribute] }
36
48
  end
49
+
50
+ def all_records
51
+ User.select(:id, :name, :city).map(&:attributes)
52
+ end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: databound
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domas Bitvinskas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-22 00:00:00.000000000 Z
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: andand
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rspec-rails
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -136,7 +122,8 @@ dependencies:
136
122
  - - "~>"
137
123
  - !ruby/object:Gem::Version
138
124
  version: '10.0'
139
- description: This is the Ruby on Rails backend part for the Databound javascript lib.
125
+ description: Ruby on Rails backend for the Databound javascript lib. Supports ActiveRecord
126
+ and Mongoid
140
127
  email:
141
128
  - domas.bitvinskas@me.com
142
129
  executables: []
@@ -153,6 +140,7 @@ files:
153
140
  - databound.gemspec
154
141
  - lib/databound.rb
155
142
  - lib/databound/data.rb
143
+ - lib/databound/extensions.rb
156
144
  - lib/databound/manager.rb
157
145
  - lib/databound/rails/routes.rb
158
146
  - lib/databound/version.rb
@@ -247,7 +235,7 @@ rubyforge_project:
247
235
  rubygems_version: 2.2.2
248
236
  signing_key:
249
237
  specification_version: 4
250
- summary: ActiveRecord exposed to the Javascript side and guarded by guns
238
+ summary: Exposes database ORM to the Javascript side
251
239
  test_files:
252
240
  - spec/controllers/databound_spec.rb
253
241
  - spec/controllers/dsl_controller_spec.rb