godfather 0.0.2
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +2 -0
- data/godfather.gemspec +23 -0
- data/lib/godfather.rb +66 -0
- data/lib/godfather/data.rb +32 -0
- data/lib/godfather/manager.rb +45 -0
- data/lib/godfather/rails/routes.rb +17 -0
- data/lib/godfather/version.rb +3 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0142891706b8057ccd00221c9e94a58cd5557a7a
|
4
|
+
data.tar.gz: 650ea8d82a17877fbcde40bed58d98a39e25f05e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eb80a2e065907ef9844d55dc0fc7c10a5f04a5c4ef760b2b117eeccbb6f2540f634ec63cb9efe1dc30bfe7123d36eaaa4bf99c50859844d4d5fd5a3cc6680a98
|
7
|
+
data.tar.gz: 48e9485326c3a2c3f69180fc53b3ff01c8e4a5c08ad7a38aff629172ac05df29f93d635bffe0460da03a2a572c85e0f3770603f32ebbb13a75a06138bb7200f7
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Domas
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Godfather
|
2
|
+
|
3
|
+
Exposes ActiveRecord records to the Javascript side.
|
4
|
+
|
5
|
+
This is the **Ruby on Rails** backend part for the ``Godfather.js`` lib.
|
6
|
+
|
7
|
+
For more information go to [Godfather.js repo](https://github.com/Nedomas/godfather.js).
|
8
|
+
|
9
|
+
## Javascript library
|
10
|
+
|
11
|
+
It does something like this out of the box.
|
12
|
+
|
13
|
+
```js
|
14
|
+
User = new Godfather('/users');
|
15
|
+
|
16
|
+
User.update({ id: 15, name: 'Saint John' }).then(function(updated_user) {
|
17
|
+
});
|
18
|
+
```
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
The library has two parts and has Lodash as a dependency.
|
23
|
+
|
24
|
+
#### I. Javascript part
|
25
|
+
|
26
|
+
Follow the guide on [Godfather.js repo](https://github.com/Nedomas/godfather.js).
|
27
|
+
|
28
|
+
#### II. Ruby on Rails part
|
29
|
+
|
30
|
+
**1.** Add ``gem 'godfather'`` to ``Gemfile``.
|
31
|
+
|
32
|
+
**2.** Create a controller with method ``model`` which returns the model to be accessed.
|
33
|
+
Also include ``Godfather::Controller``
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class UsersController < ApplicationController
|
37
|
+
include Godfather
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def model
|
42
|
+
User
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
**3.** Add a route to ``routes.rb``
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# This creates POST routes on /users to UsersController
|
51
|
+
# For where, create, update, destroy
|
52
|
+
|
53
|
+
godfather_of :users
|
54
|
+
```
|
55
|
+
|
56
|
+
## Additional features
|
57
|
+
|
58
|
+
All features are described in [Godfather.js repo](https://github.com/Nedomas/godfather.js).
|
data/Rakefile
ADDED
data/godfather.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'godfather/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'godfather'
|
8
|
+
spec.version = Godfather::VERSION
|
9
|
+
spec.authors = ['Domas Bitvinskas']
|
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 Godfather.js lib.}
|
13
|
+
spec.homepage = 'https://github.com/Nedomas/godfather'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
end
|
data/lib/godfather.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'godfather/version'
|
2
|
+
require 'godfather/data'
|
3
|
+
require 'godfather/manager'
|
4
|
+
require 'godfather/rails/routes'
|
5
|
+
|
6
|
+
module Godfather
|
7
|
+
def self.included(base)
|
8
|
+
base.send(:before_action, :init_crud, only: %i(where create update destroy))
|
9
|
+
end
|
10
|
+
|
11
|
+
def where
|
12
|
+
records = @crud.find_scoped_records
|
13
|
+
|
14
|
+
render json: serialized(records)
|
15
|
+
end
|
16
|
+
|
17
|
+
def create
|
18
|
+
record = @crud.create_from_data
|
19
|
+
|
20
|
+
render json: {
|
21
|
+
success: true,
|
22
|
+
id: record.id,
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def update
|
27
|
+
record = @crud.update_from_data
|
28
|
+
|
29
|
+
render json: {
|
30
|
+
success: true,
|
31
|
+
id: record.id,
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def destroy
|
36
|
+
@crud.destroy_from_data
|
37
|
+
|
38
|
+
render json: {
|
39
|
+
success: true,
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def serialized(records)
|
46
|
+
return records unless defined?(ActiveModel::Serializer)
|
47
|
+
|
48
|
+
serializer = ActiveModel::Serializer.serializer_for(records.first)
|
49
|
+
return records unless serializer
|
50
|
+
|
51
|
+
ActiveModel::ArraySerializer.new(records).to_json
|
52
|
+
end
|
53
|
+
|
54
|
+
def model
|
55
|
+
raise 'Override model method to specify a model to be used in CRUD'
|
56
|
+
end
|
57
|
+
|
58
|
+
def override!(name, value, data)
|
59
|
+
value
|
60
|
+
end
|
61
|
+
|
62
|
+
def init_crud
|
63
|
+
@crud = Godfather::Manager.new(self, model, params[:scope],
|
64
|
+
params[:data], params[:extra_find_scopes])
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Godfather
|
2
|
+
class Data
|
3
|
+
def initialize(controller, json)
|
4
|
+
@controller = controller
|
5
|
+
|
6
|
+
return unless json
|
7
|
+
hash = JSON.parse(json) if json.is_a?(String)
|
8
|
+
hash = json if json.is_a?(Hash)
|
9
|
+
@data = with_overrides(hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
def records(model)
|
13
|
+
model.where(@data)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_h
|
17
|
+
@data
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def with_overrides(hash)
|
23
|
+
result = {}
|
24
|
+
|
25
|
+
hash.each do |key, val|
|
26
|
+
result[key] = @controller.send(:override!, key.to_sym, val, hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Godfather
|
2
|
+
class Manager
|
3
|
+
def initialize(controller, model, scope_js, data_js, extra_find_scopes_js = '[]')
|
4
|
+
@model = model
|
5
|
+
@scope = Godfather::Data.new(controller, scope_js)
|
6
|
+
@data = Godfather::Data.new(controller, data_js).to_h
|
7
|
+
|
8
|
+
@extra_find_scopes = JSON.parse(extra_find_scopes_js).map do |extra_scope|
|
9
|
+
Godfather::Data.new(controller, extra_scope)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def find_scoped_records
|
14
|
+
records = []
|
15
|
+
records << @scope.records(@model)
|
16
|
+
|
17
|
+
@extra_find_scopes.each do |extra_scope|
|
18
|
+
records << extra_scope.records(@model)
|
19
|
+
end
|
20
|
+
|
21
|
+
records.map { |record| record.where(@data) }.flatten
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_from_data
|
25
|
+
@model.where(@scope.to_h).create(@data)
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_from_data
|
29
|
+
new_data = @data
|
30
|
+
id = new_data.delete('id')
|
31
|
+
|
32
|
+
permitted_cols = @model.column_names
|
33
|
+
updatable_data = new_data.slice(*permitted_cols)
|
34
|
+
|
35
|
+
record = @model.find(id)
|
36
|
+
record.update(updatable_data)
|
37
|
+
|
38
|
+
record
|
39
|
+
end
|
40
|
+
|
41
|
+
def destroy_from_data
|
42
|
+
@model.find(@data['id']).destroy
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class ActionDispatch::Routing::Mapper
|
2
|
+
def godfather_of(*resources)
|
3
|
+
namespace = @scope[:path]
|
4
|
+
namespace = namespace[1..-1] if namespace
|
5
|
+
|
6
|
+
resources.each do |resource|
|
7
|
+
Rails.application.routes.draw do
|
8
|
+
%i(where create update destroy).each do |name|
|
9
|
+
path = [namespace, resource, name].compact.join('/')
|
10
|
+
controller = [namespace, resource].compact.join('/')
|
11
|
+
to = [controller, name].join('#')
|
12
|
+
post path => to
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: godfather
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Domas Bitvinskas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: This is the Ruby on Rails backend part for the Godfather.js lib.
|
42
|
+
email:
|
43
|
+
- domas.bitvinskas@me.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- godfather.gemspec
|
54
|
+
- lib/godfather.rb
|
55
|
+
- lib/godfather/data.rb
|
56
|
+
- lib/godfather/manager.rb
|
57
|
+
- lib/godfather/rails/routes.rb
|
58
|
+
- lib/godfather/version.rb
|
59
|
+
homepage: https://github.com/Nedomas/godfather
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: ActiveRecord exposed to the Javascript side and guarded by guns
|
83
|
+
test_files: []
|
84
|
+
has_rdoc:
|