resourcify 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 +4 -4
- data/README.md +8 -10
- data/lib/resourcify.rb +0 -3
- data/lib/resourcify/controller/actions/create.rb +7 -9
- data/lib/resourcify/controller/actions/destroy.rb +7 -9
- data/lib/resourcify/controller/actions/index.rb +8 -18
- data/lib/resourcify/controller/actions/show.rb +2 -5
- data/lib/resourcify/controller/actions/update.rb +7 -9
- data/lib/resourcify/controller/base.rb +12 -31
- data/lib/resourcify/model/filter_by.rb +33 -0
- data/lib/resourcify/resourcify.rb +10 -10
- data/lib/resourcify/version.rb +1 -1
- metadata +5 -19
- data/lib/resourcify/model/resourcify_filter.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 024f5520a2238ae49d504aa8cc020fa538bb1819
|
4
|
+
data.tar.gz: 16c2a53c2a5d639ee0bc48f2a3c4f1dd0ad3df1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8d915890438f9f14eb391c44c5440d84c7216764f79bce73bddc69e31e790358e7b5ae6030db19beb014f1e4ee81aa52c56376009ed5388182dc5f245752df0
|
7
|
+
data.tar.gz: b205151f540d8c63c841fed008b6c315b2cfc16dc39b541c0f2ca6499bdffe467845aa36d1efe00819503af1c94d97a8fcf23e04ab1767a17d2a92ecc285e98f
|
data/README.md
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
# Resourcify [![Gem Version](https://badge.fury.io/rb/resourcify.png)](http://badge.fury.io/rb/resourcify)
|
2
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 "
|
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 "filter_by" method. This gem behaves as an "acts_as" gem by using ActiveSupport Concerns.
|
4
4
|
|
5
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)
|
6
|
+
The resourcify gem currently depends on [Pundit](https://github.com/elabs/pundit)
|
9
7
|
|
10
8
|
## Installation
|
11
9
|
|
@@ -98,19 +96,19 @@ end
|
|
98
96
|
This allows you to filter your models like this
|
99
97
|
```ruby
|
100
98
|
# Post with title equal to 'My First Post'
|
101
|
-
Post.
|
99
|
+
Post.filter_by('title' => 'My First Post')
|
102
100
|
|
103
101
|
# Users with first_name like 'Jo'
|
104
|
-
User.
|
102
|
+
User.filter_by('first_name.like' => 'Jo')
|
105
103
|
|
106
104
|
# The following parameters are allowed:
|
107
105
|
# [eq(=), ne(!=), like(LIKE), lt(<), gt(>), lte(<=), gte(>=), in(IN [items]), nin(NOT IN [items])]
|
108
|
-
User.
|
109
|
-
User.
|
110
|
-
User.
|
106
|
+
User.filter_by('first_name.like' => 'Jo', 'last_name.eq' => 'Doe')
|
107
|
+
User.filter_by('age.gt' => 37)
|
108
|
+
User.filter_by('age.gte' => 21, 'age.lte' => 35)
|
111
109
|
|
112
110
|
# Users with age in [25, 26, 52, 62]
|
113
|
-
User.
|
111
|
+
User.filter_by('age.in' => '25,26,52,62')
|
114
112
|
```
|
115
113
|
|
116
114
|
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.
|
data/lib/resourcify.rb
CHANGED
@@ -6,17 +6,15 @@ module Controller::Actions
|
|
6
6
|
authorize @record
|
7
7
|
|
8
8
|
if @record.save
|
9
|
-
|
10
|
-
@response_data[:data] = @record
|
9
|
+
render json: @record
|
11
10
|
else
|
12
|
-
@
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
}
|
17
|
-
end
|
11
|
+
@error[:type] = 'Validation'
|
12
|
+
@error[:message] = 'Sorry, there were validation errors.'
|
13
|
+
@error[:errors] = @record.errors.messages
|
14
|
+
@error[:messages] = @record.errors.full_messages
|
18
15
|
|
19
|
-
|
16
|
+
render json: @error
|
17
|
+
end
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
@@ -4,17 +4,15 @@ module Controller::Actions
|
|
4
4
|
authorize @record
|
5
5
|
|
6
6
|
if @record.destroy
|
7
|
-
|
8
|
-
@response_data[:data] = @record
|
7
|
+
render json: @record
|
9
8
|
else
|
10
|
-
@
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
}
|
15
|
-
end
|
9
|
+
@error[:type] = 'Validation'
|
10
|
+
@error[:message] = 'Sorry, there were validation errors.'
|
11
|
+
@error[:errors] = @record.errors.messages
|
12
|
+
@error[:messages] = @record.errors.full_messages
|
16
13
|
|
17
|
-
|
14
|
+
render json: @error
|
15
|
+
end
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
@@ -3,30 +3,20 @@ module Controller::Actions
|
|
3
3
|
def index
|
4
4
|
authorize _RC.new
|
5
5
|
|
6
|
-
|
7
|
-
recs = policy_scope(_RC.includes(belongs_tos))
|
8
|
-
# recs = policy_scope(_RC.includes([]).all)
|
6
|
+
@records = policy_scope(_RC.includes(belongs_tos))
|
9
7
|
|
10
|
-
# apply
|
11
|
-
if
|
12
|
-
|
8
|
+
# apply filter_by if present
|
9
|
+
if @records.respond_to? "filter_by"
|
10
|
+
@records = @records.filter_by(params.except(:controller, :action, :page, :size))
|
13
11
|
end
|
14
12
|
|
15
|
-
|
13
|
+
response.headers['_meta_total'] = @records.count.to_s
|
16
14
|
|
17
15
|
page = params[:page] || 1
|
18
|
-
size = params[:size] ||
|
19
|
-
|
16
|
+
size = params[:size] || 20
|
17
|
+
@records = @records.offset((page.to_i - 1) * size.to_i).limit(size)
|
20
18
|
|
21
|
-
|
22
|
-
@response_data[:success] = true
|
23
|
-
@response_data[:data] = {
|
24
|
-
total: recs_total,
|
25
|
-
rows: recs
|
26
|
-
}
|
27
|
-
end
|
28
|
-
|
29
|
-
render json: @response_data
|
19
|
+
render json: @records
|
30
20
|
end
|
31
21
|
end
|
32
22
|
end
|
@@ -4,17 +4,15 @@ module Controller::Actions
|
|
4
4
|
authorize @record
|
5
5
|
|
6
6
|
if @record.update(permitted_params)
|
7
|
-
|
8
|
-
@response_data[:data] = @record
|
7
|
+
render json: @record
|
9
8
|
else
|
10
|
-
@
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
}
|
15
|
-
end
|
9
|
+
@error[:type] = 'Validation'
|
10
|
+
@error[:message] = 'Sorry, there were validation errors.'
|
11
|
+
@error[:errors] = @record.errors.messages
|
12
|
+
@error[:messages] = @record.errors.full_messages
|
16
13
|
|
17
|
-
|
14
|
+
render json: @error
|
15
|
+
end
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
@@ -1,49 +1,30 @@
|
|
1
1
|
module Controller
|
2
2
|
module Base
|
3
|
-
require 'active_record/serializer_override'
|
4
|
-
ActiveRecord::Base.send(:include, ActiveRecord::SerializerOverride)
|
5
3
|
|
6
4
|
include Pundit
|
7
5
|
|
8
6
|
private
|
9
|
-
# Set
|
10
|
-
def
|
11
|
-
@
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
# Set error response data
|
8
|
+
def set_error
|
9
|
+
@error = {
|
10
|
+
error: true,
|
11
|
+
type: 'Error',
|
12
|
+
message: 'Sorry, an error occurred.'
|
15
13
|
}
|
16
|
-
# raise Resourcify::UndefinedError unless _RC.respond_to? 'resourcified?'
|
17
14
|
end
|
18
15
|
|
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
|
-
|
26
|
-
# render json: @response_data
|
27
|
-
# end
|
28
|
-
|
29
16
|
def record_not_found
|
30
|
-
@
|
31
|
-
@
|
32
|
-
type: 'record_not_found',
|
33
|
-
messages: [ 'Sorry, the record was not found.' ]
|
34
|
-
}
|
17
|
+
@error[:type] = 'RecordNotFound'
|
18
|
+
@error[:message] = 'Sorry, the record was not found.'
|
35
19
|
|
36
|
-
render json: @
|
20
|
+
render json: @error
|
37
21
|
end
|
38
22
|
|
39
23
|
def user_not_authorized
|
40
|
-
@
|
41
|
-
@
|
42
|
-
type: 'user_not_authorized',
|
43
|
-
messages: [ 'Sorry, you do not have the permission.' ]
|
44
|
-
}
|
24
|
+
@error[:type] = 'UserNotAuthorized'
|
25
|
+
@error[:message] = 'Sorry, you do not have the permission.'
|
45
26
|
|
46
|
-
render json: @
|
27
|
+
render json: @error
|
47
28
|
end
|
48
29
|
|
49
30
|
def _RC
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Model
|
2
|
+
module FilterBy
|
3
|
+
def filter_by(filters = {})
|
4
|
+
records = self
|
5
|
+
column_names = self.column_names
|
6
|
+
simple_ops = { eq: '=', lt: '<', gt: '>', lte: '<=', gte: '>=' }
|
7
|
+
filters.select { |e| column_names.include?(e.split('.').first) }.each do |key, value|
|
8
|
+
field, operator = key.split('.')
|
9
|
+
operator = (operator)? operator.to_sym : :eq
|
10
|
+
|
11
|
+
if simple_ops[operator]
|
12
|
+
records = records.where("#{field} #{simple_ops[operator]} ?", value)
|
13
|
+
elsif operator == :ne
|
14
|
+
records = records.where.not("#{field} = ?", value)
|
15
|
+
elsif operator == :in
|
16
|
+
records = records.where("#{field} IN (?)", value.split(','))
|
17
|
+
elsif operator == :nin
|
18
|
+
records = records.where.not("#{field} IN (?)", value.split(','))
|
19
|
+
elsif operator == :like
|
20
|
+
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
21
|
+
records = records.where("#{field} ILIKE ?", "%#{value}%")
|
22
|
+
else
|
23
|
+
records = records.where("#{field} LIKE ?", "%#{value}%")
|
24
|
+
end
|
25
|
+
else
|
26
|
+
records = records.where("#{field} = ?", value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
records
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require "resourcify/model/resourcify_filter"
|
2
|
-
require "resourcify/model/policy_class"
|
3
1
|
require "resourcify/model/tpl"
|
2
|
+
require "resourcify/model/filter_by"
|
3
|
+
require "resourcify/model/policy_class"
|
4
4
|
require "resourcify/controller/base"
|
5
5
|
require "resourcify/controller/actions/index"
|
6
6
|
require "resourcify/controller/actions/create"
|
@@ -21,15 +21,14 @@ module Resourcify
|
|
21
21
|
def resourcified?() true end
|
22
22
|
|
23
23
|
if self.ancestors.include?(ActiveRecord::Base) # models
|
24
|
-
#
|
25
|
-
send :extend, Model::
|
26
|
-
def filterable?() true end
|
24
|
+
# Add tpl methods
|
25
|
+
send :extend, Model::Tpl
|
27
26
|
|
28
27
|
# Add policy_class method for pundit
|
29
28
|
send :extend, Model::PolicyClass
|
30
29
|
|
31
|
-
#
|
32
|
-
send :extend, Model::
|
30
|
+
# Include filter_by
|
31
|
+
send :extend, Model::FilterBy
|
33
32
|
|
34
33
|
# Include instance methods
|
35
34
|
send :include, ModelInstanceMethods
|
@@ -40,14 +39,15 @@ module Resourcify
|
|
40
39
|
# Respond to only json requests
|
41
40
|
respond_to :json
|
42
41
|
|
43
|
-
# Set
|
44
|
-
before_action :
|
42
|
+
# Set error with set_error method located in base.rb
|
43
|
+
before_action :set_error
|
44
|
+
|
45
|
+
# Set record with set_record method located in base.rb
|
45
46
|
before_action :set_record, only: [:show, :update, :destroy]
|
46
47
|
|
47
48
|
# Set rescue_froms with methods located in base.rb
|
48
49
|
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
|
49
50
|
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
50
|
-
# rescue_from UndefinedError, with: :resource_not_resourcified
|
51
51
|
|
52
52
|
# Include base.rb with before_action filters & rescue_from methods
|
53
53
|
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.1.0
|
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-04-
|
11
|
+
date: 2014-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 4.0.0
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: active_model_serializers
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.8.1
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ~>
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 0.8.1
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: pundit
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,7 +52,7 @@ dependencies:
|
|
66
52
|
- - '>='
|
67
53
|
- !ruby/object:Gem::Version
|
68
54
|
version: '0'
|
69
|
-
description:
|
55
|
+
description: Resourcify rails controllers and models
|
70
56
|
email:
|
71
57
|
- stephen@axoninfosystems.com
|
72
58
|
executables: []
|
@@ -83,8 +69,8 @@ files:
|
|
83
69
|
- lib/resourcify/controller/actions/show.rb
|
84
70
|
- lib/resourcify/controller/actions/update.rb
|
85
71
|
- lib/resourcify/controller/base.rb
|
72
|
+
- lib/resourcify/model/filter_by.rb
|
86
73
|
- lib/resourcify/model/policy_class.rb
|
87
|
-
- lib/resourcify/model/resourcify_filter.rb
|
88
74
|
- lib/resourcify/model/tpl.rb
|
89
75
|
- lib/resourcify/resourcify.rb
|
90
76
|
- lib/resourcify/version.rb
|
@@ -159,7 +145,7 @@ rubyforge_project:
|
|
159
145
|
rubygems_version: 2.2.2
|
160
146
|
signing_key:
|
161
147
|
specification_version: 4
|
162
|
-
summary:
|
148
|
+
summary: Resourcify rails controllers and models
|
163
149
|
test_files:
|
164
150
|
- test/acts_as_resourcify_test.rb
|
165
151
|
- test/dummy/app/assets/javascripts/application.js
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Model
|
2
|
-
module ResourcifyFilter
|
3
|
-
def resourcify_filter(filter_string)
|
4
|
-
records = self
|
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
|
-
|
10
|
-
filter_string.each do |f|
|
11
|
-
next if f[:value].blank?
|
12
|
-
|
13
|
-
operand = f[:op].to_s.to_sym
|
14
|
-
|
15
|
-
if simple_ops[operand]
|
16
|
-
f[:value] = f[:value].to_time if f[:type] == "date"
|
17
|
-
records = records.where("#{f[:name]} #{simple_ops[operand]} ?", f[:value])
|
18
|
-
elsif operand == :like
|
19
|
-
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
20
|
-
records = records.where("#{f[:name]} ILIKE ?", "%#{f[:value]}%")
|
21
|
-
else
|
22
|
-
records = records.where("#{f[:name]} LIKE ?", "%#{f[:value]}%")
|
23
|
-
end
|
24
|
-
elsif operand == :ne
|
25
|
-
records = records.where.not("#{f[:name]} = ?", f[:value])
|
26
|
-
elsif operand == :in
|
27
|
-
records = records.where("#{f[:name]} IN (?)", f[:value].split(','))
|
28
|
-
elsif operand == :nin
|
29
|
-
records = records.where.not("#{f[:name]} IN (?)", f[:value].split(','))
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
records
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|