resourcify 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +120 -0
- data/lib/resourcify/controller/actions/index.rb +4 -2
- data/lib/resourcify/controller/base.rb +9 -9
- data/lib/resourcify/model/{filter.rb → resourcify_filter.rb} +10 -8
- data/lib/resourcify/resourcify.rb +3 -3
- data/lib/resourcify/version.rb +1 -1
- metadata +4 -4
- data/README.rdoc +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35ae2a1538823e8d0c2c5b48bed067a61c7617e1
|
4
|
+
data.tar.gz: 41ef92c2d410713392e2c1654a1f0b345ae5609c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f5d18268a7af685c16b8acdb6a527a12c3cb65aaf6781564cef44e807c5f516321c41591f1d7e0a5ba1a67745eb5ee363247b8739013fb899035adf0e473ee2
|
7
|
+
data.tar.gz: c882e3506a2f7920bdd1b7edd3bfeee72c0cf6358ed2293c893591a7f2bdf7651845e29775a538b9e716e4368bd1a8c77cfa06329add29636e4106fb3b1bc848
|
data/MIT-LICENSE
CHANGED
data/README.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# Resourcify
|
2
|
+
|
3
|
+
Resourcify is a rails gem that helps to speed up development by giving you json api controllers that inherit all restful actions. It also makes your models easier to filter by adding a "resourcify_filter" method. This gem behaves as an "acts_as" gem by using ActiveSupport Concerns.
|
4
|
+
|
5
|
+
#### Caveat
|
6
|
+
The resourcify gem currently depends on
|
7
|
+
* [ActiveModel::Serializers](https://github.com/rails-api/active_model_serializers)
|
8
|
+
* [Pundit](https://github.com/elabs/pundit)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
### Rails 4
|
13
|
+
|
14
|
+
Include the gem in your Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'resourcify'
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Applies to different parts of Rails:
|
23
|
+
|
24
|
+
* [Controllers](#controllers)
|
25
|
+
* [Models](#models)
|
26
|
+
|
27
|
+
### Controllers
|
28
|
+
|
29
|
+
Usage with controllers is very easy. Just add "resourcify" to your controller and your controller will inherit all the RESTful actions with json response.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class PostsController < ApplicationController
|
33
|
+
# Include the resourcify module
|
34
|
+
resourcify
|
35
|
+
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
You can check the [base.rb file](https://github.com/stephenbaidu/resourcify/blob/master/lib/resourcify/controller/base.rb) to see the private methods that have been made available.
|
40
|
+
|
41
|
+
#### Strong Parameters
|
42
|
+
|
43
|
+
By default, your controller has a permitted_params method for rails strong parameters like this. "_RC" is the resource class or model class.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# Only allow a trusted parameter "white list" through.
|
47
|
+
def permitted_params
|
48
|
+
if self.respond_to? "#{controller_name.singularize}_params", true
|
49
|
+
self.send("#{controller_name.singularize}_params")
|
50
|
+
else
|
51
|
+
param_key = _RC.name.split('::').last.singularize.underscore.to_sym
|
52
|
+
excluded_fields = ["id", "created_at", "updated_at"]
|
53
|
+
permitted_fields = (_RC.column_names - excluded_fields).map { |f| f.to_sym }
|
54
|
+
params.fetch(param_key, {}).permit([]).tap do |wl|
|
55
|
+
permitted_fields.each { |f| wl[f] = params[param_key][f] if params[param_key].key?(f) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
You can override this by defining a custom method called "permitted_params" (or "post_params" for PostsController) in your controller.
|
61
|
+
|
62
|
+
### Models
|
63
|
+
|
64
|
+
Asuming you have the following models (Post, User) in your application.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class Post < ActiveRecord::Base
|
68
|
+
attr_accessible :title
|
69
|
+
|
70
|
+
belongs_to :user
|
71
|
+
end
|
72
|
+
|
73
|
+
class User < ActiveRecord::Base
|
74
|
+
attr_accessible :first_name, last_name, age
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
Then you can add "resourcify" like this
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Post < ActiveRecord::Base
|
82
|
+
# Include the resourcify module
|
83
|
+
resourcify
|
84
|
+
|
85
|
+
attr_accessible :title
|
86
|
+
|
87
|
+
belongs_to :user
|
88
|
+
end
|
89
|
+
|
90
|
+
class User < ActiveRecord::Base
|
91
|
+
# Include the resourcify module
|
92
|
+
resourcify
|
93
|
+
|
94
|
+
attr_accessible :first_name, last_name, age
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
This allows you to filter your models like this
|
99
|
+
```ruby
|
100
|
+
# Post with title equal to 'My First Post'
|
101
|
+
Post.resourcify_filter("title::eq::My First Post")
|
102
|
+
|
103
|
+
# Users with first_name like 'Jo'
|
104
|
+
User.resourcify_filter("first_name::like::Jo")
|
105
|
+
|
106
|
+
# The following parameters are allowed:
|
107
|
+
# [eq(=), ne(!=), like(LIKE), lt(<), gt(>), lte(<=), gte(>=), in(IN [items]), nin(NOT IN [items])]
|
108
|
+
User.resourcify_filter("first_name::like::Jo;;last_name::eq::Doe")
|
109
|
+
User.resourcify_filter("age::gt::37")
|
110
|
+
User.resourcify_filter("age::gte::21;;age::lte::35")
|
111
|
+
|
112
|
+
# Users with age in [25, 26, 52, 62]
|
113
|
+
User.resourcify_filter("age::in::25,26,52,62")
|
114
|
+
```
|
115
|
+
|
116
|
+
Each model with "resourcify" has a "policy_class" method which returns "ApiPolicy" that can be used by a generic api controller when using Pundit. This was added since the gem is currently tied with Pundit but will later be made optional.
|
117
|
+
|
118
|
+
## License
|
119
|
+
|
120
|
+
Resourcify is free software, and may be redistributed under the terms specified in the [MIT-LICENSE](MIT-LICENSE) file.
|
@@ -5,8 +5,10 @@ module Controller::Actions
|
|
5
5
|
|
6
6
|
recs = policy_scope(_RC.all)
|
7
7
|
|
8
|
-
# apply
|
9
|
-
|
8
|
+
# apply resourcify_filter if present and query param is also present
|
9
|
+
if recs.respond_to? "resourcify_filter" and params[:query].present?
|
10
|
+
recs = recs.resourcify_filter(params[:query])
|
11
|
+
end
|
10
12
|
|
11
13
|
recs_total = recs.count
|
12
14
|
|
@@ -13,18 +13,18 @@ module Controller
|
|
13
13
|
data: { total: 0, rows: [] },
|
14
14
|
error: { type: '', errors: {}, messages: [] }
|
15
15
|
}
|
16
|
-
raise Resourcify::UndefinedError unless _RC.respond_to? 'resourcified?'
|
16
|
+
# raise Resourcify::UndefinedError unless _RC.respond_to? 'resourcified?'
|
17
17
|
end
|
18
18
|
|
19
|
-
def resource_not_resourcified
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
19
|
+
# def resource_not_resourcified
|
20
|
+
# @response_data[:success] = false
|
21
|
+
# @response_data[:error] = {
|
22
|
+
# type: 'resource_not_resourcified',
|
23
|
+
# messages: [ 'Resourcify::UndefinedError. Resource route not defined' ]
|
24
|
+
# }
|
25
25
|
|
26
|
-
|
27
|
-
end
|
26
|
+
# render json: @response_data
|
27
|
+
# end
|
28
28
|
|
29
29
|
def record_not_found
|
30
30
|
@response_data[:success] = false
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module Model
|
2
|
-
module
|
3
|
-
def
|
2
|
+
module ResourcifyFilter
|
3
|
+
def resourcify_filter(filter_string)
|
4
4
|
records = self
|
5
|
-
simple_ops = { eq: '=', lt: '<', gt: '>',
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
simple_ops = { eq: '=', lt: '<', gt: '>', lte: '<=', gte: '>=' }
|
6
|
+
filter_string = filter_string.split(';;').map { |q| q.split('::') }
|
7
|
+
filter_string = filter_string.map { |q| {name: q[0], op: q[1], value: q[2], type: q[3]} }
|
8
|
+
filter_string = filter_string.select { |f| self.column_names.include?(f[:name]) }
|
9
9
|
|
10
|
-
|
10
|
+
filter_string.each do |f|
|
11
11
|
next if f[:value].blank?
|
12
12
|
|
13
13
|
operand = f[:op].to_s.to_sym
|
@@ -21,9 +21,11 @@ module Model
|
|
21
21
|
else
|
22
22
|
records = records.where("#{f[:name]} LIKE ?", "%#{f[:value]}%")
|
23
23
|
end
|
24
|
+
elsif operand == :ne
|
25
|
+
records = records.where.not("#{f[:name]} = ?", f[:value])
|
24
26
|
elsif operand == :in
|
25
27
|
records = records.where("#{f[:name]} IN (?)", f[:value].split(','))
|
26
|
-
elsif operand == :
|
28
|
+
elsif operand == :nin
|
27
29
|
records = records.where.not("#{f[:name]} IN (?)", f[:value].split(','))
|
28
30
|
end
|
29
31
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require "resourcify/model/
|
1
|
+
require "resourcify/model/resourcify_filter"
|
2
2
|
require "resourcify/model/policy_class"
|
3
3
|
require "resourcify/controller/base"
|
4
4
|
require "resourcify/controller/actions/index"
|
@@ -21,7 +21,7 @@ module Resourcify
|
|
21
21
|
|
22
22
|
if self.ancestors.include?(ActiveRecord::Base) # models
|
23
23
|
# Include filter and tag as filterable?
|
24
|
-
send :extend, Model::
|
24
|
+
send :extend, Model::ResourcifyFilter
|
25
25
|
def filterable?() true end
|
26
26
|
|
27
27
|
# Add policy_class method for pundit
|
@@ -43,7 +43,7 @@ module Resourcify
|
|
43
43
|
# Set rescue_froms with methods located in base.rb
|
44
44
|
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
|
45
45
|
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
46
|
-
rescue_from UndefinedError, with: :resource_not_resourcified
|
46
|
+
# rescue_from UndefinedError, with: :resource_not_resourcified
|
47
47
|
|
48
48
|
# Include base.rb with before_action filters & rescue_from methods
|
49
49
|
send :include, Controller::Base
|
data/lib/resourcify/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resourcify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Baidu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -46,7 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- MIT-LICENSE
|
49
|
-
- README.
|
49
|
+
- README.md
|
50
50
|
- Rakefile
|
51
51
|
- lib/resourcify.rb
|
52
52
|
- lib/resourcify/controller/actions/create.rb
|
@@ -55,8 +55,8 @@ files:
|
|
55
55
|
- lib/resourcify/controller/actions/show.rb
|
56
56
|
- lib/resourcify/controller/actions/update.rb
|
57
57
|
- lib/resourcify/controller/base.rb
|
58
|
-
- lib/resourcify/model/filter.rb
|
59
58
|
- lib/resourcify/model/policy_class.rb
|
59
|
+
- lib/resourcify/model/resourcify_filter.rb
|
60
60
|
- lib/resourcify/resourcify.rb
|
61
61
|
- lib/resourcify/version.rb
|
62
62
|
- lib/tasks/resourcify_tasks.rake
|
data/README.rdoc
DELETED