resourcify 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +32 -0
- data/lib/resourcify.rb +7 -0
- data/lib/resourcify/controller/actions/create.rb +22 -0
- data/lib/resourcify/controller/actions/destroy.rb +20 -0
- data/lib/resourcify/controller/actions/index.rb +28 -0
- data/lib/resourcify/controller/actions/show.rb +12 -0
- data/lib/resourcify/controller/actions/update.rb +20 -0
- data/lib/resourcify/controller/base.rb +72 -0
- data/lib/resourcify/model/filter.rb +34 -0
- data/lib/resourcify/model/policy_class.rb +9 -0
- data/lib/resourcify/resourcify.rb +70 -0
- data/lib/resourcify/version.rb +3 -0
- data/lib/tasks/resourcify_tasks.rake +4 -0
- data/test/acts_as_resourcify_test.rb +16 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/foo.rb +4 -0
- data/test/dummy/app/models/foo_bar.rb +4 -0
- data/test/dummy/app/models/foo_bar_policy.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/production.rb +80 -0
- data/test/dummy/config/environments/test.rb +36 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +12 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20140301115902_create_foos.rb +9 -0
- data/test/dummy/db/migrate/20140301120518_create_foo_bars.rb +10 -0
- data/test/dummy/db/schema.rb +35 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +60 -0
- data/test/dummy/log/test.log +600 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/fixtures/foo_bars.yml +9 -0
- data/test/dummy/test/fixtures/foos.yml +7 -0
- data/test/dummy/test/models/foo_bar_test.rb +7 -0
- data/test/dummy/test/models/foo_test.rb +7 -0
- data/test/resourcify_test.rb +7 -0
- data/test/test_helper.rb +15 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b7870d7f333dea432afeed996936a2cb731f3632
|
4
|
+
data.tar.gz: 147f8c9638fc801a08ba8bb01fbadfdbbbab4401
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3dd27cdb2437bb6124174683b42b9a9cbc23e1dc777e5231c29df09ed354dd234114fc012f2eeb2624a635495ff73c6dd6b454c99233d8ad7a5b4a68f1c2d372
|
7
|
+
data.tar.gz: e68314c16e70de51aee74180094bd41f36c4c7473329a90b920d7293d9ea50b6da234392e637012b9d16192b91b556bd880ce311651f5fcfc0126a7da7f1ede5
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Resourcify'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
data/lib/resourcify.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Controller::Actions
|
2
|
+
module Create
|
3
|
+
def create
|
4
|
+
@record = _RC.new(permitted_params)
|
5
|
+
|
6
|
+
authorize @record
|
7
|
+
|
8
|
+
if @record.save
|
9
|
+
@response_data[:success] = true
|
10
|
+
@response_data[:data] = @record
|
11
|
+
else
|
12
|
+
@response_data[:error] = {
|
13
|
+
type: 'validation',
|
14
|
+
errors: @record.errors.messages,
|
15
|
+
messages: @record.errors.full_messages
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
render json: @response_data
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Controller::Actions
|
2
|
+
module Destroy
|
3
|
+
def destroy
|
4
|
+
authorize @record
|
5
|
+
|
6
|
+
if @record.destroy
|
7
|
+
@response_data[:success] = true
|
8
|
+
@response_data[:data] = @record
|
9
|
+
else
|
10
|
+
@response_data[:error] = {
|
11
|
+
type: 'other',
|
12
|
+
errors: @record.errors.messages,
|
13
|
+
messages: @record.errors.full_messages
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
render json: @response_data
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Controller::Actions
|
2
|
+
module Index
|
3
|
+
def index
|
4
|
+
authorize _RC.new
|
5
|
+
|
6
|
+
recs = policy_scope(_RC.all)
|
7
|
+
|
8
|
+
# apply filter if query is present
|
9
|
+
recs = recs.filter(params[:query]) if params[:query].present?
|
10
|
+
|
11
|
+
recs_total = recs.count
|
12
|
+
|
13
|
+
page = params[:page] || 1
|
14
|
+
size = params[:size] || 25
|
15
|
+
recs = recs.offset((page.to_i - 1) * size.to_i).limit(size)
|
16
|
+
|
17
|
+
if recs
|
18
|
+
@response_data[:success] = true
|
19
|
+
@response_data[:data] = {
|
20
|
+
total: recs_total,
|
21
|
+
rows: recs
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
render json: @response_data
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Controller::Actions
|
2
|
+
module Update
|
3
|
+
def update
|
4
|
+
authorize @record
|
5
|
+
|
6
|
+
if @record.update(permitted_params)
|
7
|
+
@response_data[:success] = true
|
8
|
+
@response_data[:data] = @record
|
9
|
+
else
|
10
|
+
@response_data[:error] = {
|
11
|
+
type: 'validation',
|
12
|
+
errors: @record.errors.messages,
|
13
|
+
messages: @record.errors.full_messages
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
render json: @response_data
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Controller
|
2
|
+
module Base
|
3
|
+
require 'active_record/serializer_override'
|
4
|
+
ActiveRecord::Base.send(:include, ActiveRecord::SerializerOverride)
|
5
|
+
|
6
|
+
include Pundit
|
7
|
+
|
8
|
+
private
|
9
|
+
# Set JSON response data
|
10
|
+
def set_response_data
|
11
|
+
@response_data = {
|
12
|
+
success: false,
|
13
|
+
data: { total: 0, rows: [] },
|
14
|
+
error: { type: '', errors: {}, messages: [] }
|
15
|
+
}
|
16
|
+
raise Resourcify::UndefinedError unless _RC.respond_to? 'resourcified?'
|
17
|
+
end
|
18
|
+
|
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
|
+
def record_not_found
|
30
|
+
@response_data[:success] = false
|
31
|
+
@response_data[:error] = {
|
32
|
+
type: 'record_not_found',
|
33
|
+
messages: [ 'Sorry, the record was not found.' ]
|
34
|
+
}
|
35
|
+
|
36
|
+
render json: @response_data
|
37
|
+
end
|
38
|
+
|
39
|
+
def user_not_authorized
|
40
|
+
@response_data[:success] = false
|
41
|
+
@response_data[:error] = {
|
42
|
+
type: 'user_not_authorized',
|
43
|
+
messages: [ 'Sorry, you do not have the permission.' ]
|
44
|
+
}
|
45
|
+
|
46
|
+
render json: @response_data
|
47
|
+
end
|
48
|
+
|
49
|
+
def _RC
|
50
|
+
controller_name.classify.constantize
|
51
|
+
end
|
52
|
+
|
53
|
+
# Use callbacks to share common setup or constraints between actions.
|
54
|
+
def set_record
|
55
|
+
@record = _RC.find(params[:id])
|
56
|
+
end
|
57
|
+
|
58
|
+
# Only allow a trusted parameter "white list" through.
|
59
|
+
def permitted_params
|
60
|
+
if self.respond_to? "#{controller_name.singularize}_params", true
|
61
|
+
self.send("#{controller_name.singularize}_params")
|
62
|
+
else
|
63
|
+
param_key = _RC.name.split('::').last.singularize.underscore.to_sym
|
64
|
+
excluded_fields = ["id", "created_at", "updated_at"]
|
65
|
+
permitted_fields = (_RC.column_names - excluded_fields).map { |f| f.to_sym }
|
66
|
+
params.fetch(param_key, {}).permit([]).tap do |wl|
|
67
|
+
permitted_fields.each { |f| wl[f] = params[param_key][f] if params[param_key].key?(f) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Model
|
2
|
+
module Filter
|
3
|
+
def filter(filters)
|
4
|
+
records = self
|
5
|
+
simple_ops = { eq: '=', lt: '<', gt: '>', lteq: '<=', gteq: '>=' }
|
6
|
+
filters = filters.split(';;').map { |q| q.split('::') }
|
7
|
+
filters = filters.map { |q| {name: q[0], op: q[1], value: q[2], type: q[3]} }
|
8
|
+
filters = filters.select { |f| self.column_names.include?(f[:name]) }
|
9
|
+
|
10
|
+
filters.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 == :in
|
25
|
+
records = records.where("#{f[:name]} IN (?)", f[:value].split(','))
|
26
|
+
elsif operand == :notin
|
27
|
+
records = records.where.not("#{f[:name]} IN (?)", f[:value].split(','))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
records
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "resourcify/model/filter"
|
2
|
+
require "resourcify/model/policy_class"
|
3
|
+
require "resourcify/controller/base"
|
4
|
+
require "resourcify/controller/actions/index"
|
5
|
+
require "resourcify/controller/actions/create"
|
6
|
+
require "resourcify/controller/actions/show"
|
7
|
+
require "resourcify/controller/actions/update"
|
8
|
+
require "resourcify/controller/actions/destroy"
|
9
|
+
|
10
|
+
module Resourcify
|
11
|
+
module Resourcify
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
included do
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def resourcify(options = {})
|
19
|
+
# Class method to tag classes using this plugin
|
20
|
+
def resourcified?() true end
|
21
|
+
|
22
|
+
if self.ancestors.include?(ActiveRecord::Base) # models
|
23
|
+
# Include filter and tag as filterable?
|
24
|
+
send :extend, Model::Filter
|
25
|
+
def filterable?() true end
|
26
|
+
|
27
|
+
# Add policy_class method for pundit
|
28
|
+
send :extend, Model::PolicyClass
|
29
|
+
|
30
|
+
# Include instance methods
|
31
|
+
send :include, ModelInstanceMethods
|
32
|
+
elsif self.ancestors.include?(ActionController::Base) # controllers
|
33
|
+
# Turn off layout
|
34
|
+
layout false
|
35
|
+
|
36
|
+
# Respond to only json requests
|
37
|
+
respond_to :json
|
38
|
+
|
39
|
+
# Set before_actions with methods located in base.rb
|
40
|
+
before_action :set_response_data
|
41
|
+
before_action :set_record, only: [:show, :update, :destroy]
|
42
|
+
|
43
|
+
# Set rescue_froms with methods located in base.rb
|
44
|
+
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
|
45
|
+
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
46
|
+
rescue_from UndefinedError, with: :resource_not_resourcified
|
47
|
+
|
48
|
+
# Include base.rb with before_action filters & rescue_from methods
|
49
|
+
send :include, Controller::Base
|
50
|
+
|
51
|
+
# Include RESTful actions
|
52
|
+
send :include, Controller::Actions::Index
|
53
|
+
send :include, Controller::Actions::Create
|
54
|
+
send :include, Controller::Actions::Show
|
55
|
+
send :include, Controller::Actions::Update
|
56
|
+
send :include, Controller::Actions::Destroy
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
module ModelInstanceMethods
|
62
|
+
def policy_class
|
63
|
+
self.class.policy_class
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
ActiveRecord::Base.send :include, Resourcify::Resourcify
|
70
|
+
ActionController::Base.send :include, Resourcify::Resourcify
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActsAsResourcifyTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
def test_some_model_resourcified_should_be_true
|
6
|
+
assert_equal true, Foo.new.resourcified?
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_pundit_api_policy_class_name_on_model_without_policy_class
|
10
|
+
assert_equal "ApiPolicy", Foo.new.policy_class
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_pundit_custom_policy_class_name_on_model_with_policy_class
|
14
|
+
assert_equal "FooBarPolicy", FooBar.new.policy_class
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED