rails-add_ons 0.2.0
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/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +24 -0
- data/app/assets/config/rails_add_ons_manifest.js +0 -0
- data/app/assets/javascripts/rails/add_ons/application/widgets.js.coffee +48 -0
- data/app/assets/javascripts/rails/add_ons/application.js +17 -0
- data/app/assets/javascripts/rails_add_ons.js +1 -0
- data/app/assets/stylesheets/rails/add_ons/application/bootstrap_overrides.scss +3 -0
- data/app/assets/stylesheets/rails/add_ons/application/table_monospaced.scss +3 -0
- data/app/assets/stylesheets/rails/add_ons/application/widgets.scss +5 -0
- data/app/assets/stylesheets/rails/add_ons/application.scss +4 -0
- data/app/assets/stylesheets/rails/add_ons/bootstrap_v3_flex_extension.scss +13 -0
- data/app/assets/stylesheets/rails/add_ons/font_awesome.css +3 -0
- data/app/assets/stylesheets/rails_add_ons.css +3 -0
- data/app/caches/api/cache.rb +24 -0
- data/app/components/component/base.rb +24 -0
- data/app/components/component/collection_table.rb +80 -0
- data/app/components/component/resource_table.rb +25 -0
- data/app/concerns/api_controller_concerns/exception_handling.rb +24 -0
- data/app/concerns/resources_controller/pagination.rb +15 -0
- data/app/concerns/resources_controller/sorting.rb +15 -0
- data/app/controllers/api/resources_controller/base.rb +254 -0
- data/app/controllers/api/service_controller/base.rb +73 -0
- data/app/controllers/rails/add_ons/widgets_controller.rb +10 -0
- data/app/controllers/resources_controller/base.rb +180 -0
- data/app/controllers/service_controller/base.rb +135 -0
- data/app/extensions/object_extensions.rb +8 -0
- data/app/helpers/rails/add_ons/table_helper.rb +50 -0
- data/app/helpers/rails/add_ons/widget_helper.rb +10 -0
- data/app/parsers/api/resources_controller/condition_parser.rb +65 -0
- data/app/services/rails/add_ons/service/base.rb +156 -0
- data/app/services/rails/add_ons/service/result/base.rb +25 -0
- data/app/views/component/_collection_table.haml +20 -0
- data/app/views/component/_resource_table.haml +15 -0
- data/app/views/frontend/_navbar.haml +23 -0
- data/app/views/layouts/rails/add_ons/application.haml +23 -0
- data/app/views/resources_controller/base/_after_show_table.haml +0 -0
- data/app/views/resources_controller/base/_before_show_table.haml +0 -0
- data/app/views/resources_controller/base/_form.haml +2 -0
- data/app/views/resources_controller/base/_form_buttons.haml +4 -0
- data/app/views/resources_controller/base/_form_errors.haml +6 -0
- data/app/views/resources_controller/base/_pagination.haml +4 -0
- data/app/views/resources_controller/base/_show.haml +2 -0
- data/app/views/resources_controller/base/_show_actions.haml +6 -0
- data/app/views/resources_controller/base/_table.haml +2 -0
- data/app/views/resources_controller/base/_table_actions.haml +12 -0
- data/app/views/resources_controller/base/edit.haml +6 -0
- data/app/views/resources_controller/base/index.haml +13 -0
- data/app/views/resources_controller/base/new.haml +6 -0
- data/app/views/resources_controller/base/show.haml +10 -0
- data/app/views/service_controller/base/_create_after_service_output.haml +0 -0
- data/app/views/service_controller/base/_create_before_service_output.haml +0 -0
- data/app/views/service_controller/base/_form_buttons.haml +2 -0
- data/app/views/service_controller/base/_form_errors.haml +6 -0
- data/app/views/service_controller/base/create.haml +14 -0
- data/app/views/service_controller/base/new.haml +6 -0
- data/app/views/widget/base/_remote_load_code.html.erb +4 -0
- data/app/views/widget/base/_widget_controls.haml +3 -0
- data/app/views/widget/render.js.erb +0 -0
- data/app/widgets/widget/base.rb +92 -0
- data/config/initializers/assets.rb +7 -0
- data/config/initializers/extend_object.rb +1 -0
- data/config/locales/de.yml +39 -0
- data/config/locales/en.yml +39 -0
- data/config/routes.rb +5 -0
- data/lib/rails/add_ons/engine.rb +7 -0
- data/lib/rails/add_ons/version.rb +5 -0
- data/lib/rails/add_ons.rb +7 -0
- data/lib/rails-add_ons.rb +8 -0
- data/lib/tasks/rails/add_ons_tasks.rake +4 -0
- metadata +268 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 963bb8277207578ae5916394485eb2adb8c0e8ea
|
4
|
+
data.tar.gz: 1ab4b5ac4548ec0f88f66cf99ab712c22d88f331
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7e66a7c872436641d6f8e28c2b23d891d3b2c0122153e68ad57b2ab9817d56239d00dd42ceeec001c337740c371201db922c233ee3e27be0ea8a13896a664a2
|
7
|
+
data.tar.gz: d09be621db5157cac495193403b0e8e88f9672c3b424f0aefdc1f0e14163db67e5ccb478c79b9fefa3d9e563efbe6ee089749ca22c0b98d6f97df1d229ad225a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Roberto Vasquez Angel
|
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.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Rails::AddOns
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'rails-add_ons'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install rails-add_ons
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
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 = 'Rails::AddOns'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
load 'rails/tasks/statistics.rake'
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
require 'bundler/gem_tasks'
|
24
|
+
|
File without changes
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class window.WidgetReloader
|
2
|
+
constructor: (container_id, widget_name, widget_action)->
|
3
|
+
@container_id = container_id
|
4
|
+
@widget_name = widget_name
|
5
|
+
@widget_action = widget_action
|
6
|
+
|
7
|
+
reload: ->
|
8
|
+
console.log "Loading #{this.container_id}"
|
9
|
+
$("##{this.container_id}").html("Loading...")
|
10
|
+
$.ajax
|
11
|
+
context: this
|
12
|
+
type: 'POST'
|
13
|
+
url: this.uri()
|
14
|
+
headers:
|
15
|
+
Accept: 'application/json'
|
16
|
+
'X-CSRF-Token': this.csrf_token()
|
17
|
+
success: (data) ->
|
18
|
+
$("##{this.container_id}").html data
|
19
|
+
return
|
20
|
+
error: (jqXHR) ->
|
21
|
+
msg = 'Sorry but there was an error: '
|
22
|
+
$("##{this.container_id}").html msg + jqXHR.status + ' ' + jqXHR.statusText
|
23
|
+
return
|
24
|
+
|
25
|
+
csrf_token: ->
|
26
|
+
$('meta[name=\'csrf-token\']').attr('content')
|
27
|
+
|
28
|
+
encoded_widget_action_and_name: ->
|
29
|
+
encodeURIComponent("#{this.widget_name}##{this.widget_action}")
|
30
|
+
|
31
|
+
encoded_widget_action: ->
|
32
|
+
encodeURIComponent(this.widget_action)
|
33
|
+
|
34
|
+
base_path: ->
|
35
|
+
$('meta[name=\'widget-base-path\']').attr('content')
|
36
|
+
|
37
|
+
uri: ->
|
38
|
+
"#{this.base_path()}/#{this.encoded_widget_action_and_name()}"
|
39
|
+
|
40
|
+
$(document).ready ->
|
41
|
+
$('body').on 'click', "*[data-refresh]", ->
|
42
|
+
button_id = $(this).attr('id')
|
43
|
+
container_id = $(this).attr('data-refresh')
|
44
|
+
widget_name = $("##{container_id}").attr('data-widget')
|
45
|
+
widget_action = $("##{container_id}").attr('data-widgetaction')
|
46
|
+
wr = new window.WidgetReloader(container_id, widget_name, widget_action)
|
47
|
+
wr.reload()
|
48
|
+
return
|
@@ -0,0 +1,17 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require jquery_ujs
|
15
|
+
//= require tether
|
16
|
+
//= require bootstrap-sprockets
|
17
|
+
//= require_tree ./application
|
@@ -0,0 +1 @@
|
|
1
|
+
//= require rails/add_ons/application
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Api
|
2
|
+
class Cache
|
3
|
+
def self.cache(options = {}, &block)
|
4
|
+
instance = new(options = {})
|
5
|
+
instance.prepare_cache(&block)
|
6
|
+
instance
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(options = {}, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def prepare_cache(&block)
|
13
|
+
@cachable = block
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(m, *args, &block)
|
17
|
+
key = "#{m}-#{args.to_json}-#{block.to_json}"
|
18
|
+
# raise key
|
19
|
+
output = @cachable.call.send(m, *args, &block)
|
20
|
+
# raise output.inspect
|
21
|
+
output
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Component
|
2
|
+
class Base
|
3
|
+
def initialize(view, options = {}, &block)
|
4
|
+
@view = view
|
5
|
+
@block = block
|
6
|
+
@options = options
|
7
|
+
@view_locals = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def perform
|
11
|
+
@block_output = block_output
|
12
|
+
@view.render partial: self.class.name.underscore, locals: view_locals
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :view_locals
|
18
|
+
|
19
|
+
def block_output
|
20
|
+
return unless @block.present?
|
21
|
+
@view.capture { @block.call(self) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Component
|
2
|
+
class CollectionTable < Base
|
3
|
+
SIZE_MAP = {
|
4
|
+
default: nil,
|
5
|
+
small: :sm
|
6
|
+
}
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
super
|
10
|
+
@columns = {}
|
11
|
+
@collection = @options.delete(:collection)
|
12
|
+
@resource_class = @collection.first.class
|
13
|
+
end
|
14
|
+
|
15
|
+
def column(name, options = {}, &block)
|
16
|
+
options.reverse_merge!(block: block) if block_given?
|
17
|
+
@columns[name] = options
|
18
|
+
end
|
19
|
+
|
20
|
+
def timestamps
|
21
|
+
column(:created_at)
|
22
|
+
column(:updated_at)
|
23
|
+
end
|
24
|
+
|
25
|
+
def association(name, options = {}, &block)
|
26
|
+
options.reverse_merge!(block: block) if block_given?
|
27
|
+
@columns[name] = options
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def table
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def view_locals
|
37
|
+
{
|
38
|
+
columns: @columns,
|
39
|
+
collection: @collection,
|
40
|
+
resource_class: @resource_class,
|
41
|
+
table_css_classes: table_css_classes
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def striped?
|
46
|
+
!!@options[:striped]
|
47
|
+
end
|
48
|
+
|
49
|
+
def responsive?
|
50
|
+
!!@options[:responsive]
|
51
|
+
end
|
52
|
+
|
53
|
+
def inverse?
|
54
|
+
!!@options[:inverse]
|
55
|
+
end
|
56
|
+
|
57
|
+
def bordered?
|
58
|
+
!!@options[:bordered]
|
59
|
+
end
|
60
|
+
|
61
|
+
def hover?
|
62
|
+
!!@options[:hover]
|
63
|
+
end
|
64
|
+
|
65
|
+
def size
|
66
|
+
SIZE_MAP[@options[:size] || :default]
|
67
|
+
end
|
68
|
+
|
69
|
+
def table_css_classes
|
70
|
+
classes = ['table']
|
71
|
+
classes << 'table-bordered' if bordered?
|
72
|
+
classes << 'table-hover' if hover?
|
73
|
+
classes << 'table-inverse' if inverse?
|
74
|
+
classes << 'table-striped' if striped?
|
75
|
+
classes << 'table-responsive' if responsive?
|
76
|
+
classes << "table-#{size}" if size.present?
|
77
|
+
classes
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Component
|
2
|
+
class ResourceTable < Base
|
3
|
+
def initialize(*args)
|
4
|
+
super
|
5
|
+
@rows = {}
|
6
|
+
@resource = @options.delete(:resource)
|
7
|
+
@resource_class = @resource.class
|
8
|
+
end
|
9
|
+
|
10
|
+
def row(name, options = {}, &block)
|
11
|
+
options.reverse_merge!(block: block) if block_given?
|
12
|
+
@rows[name] = options
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def view_locals
|
18
|
+
{
|
19
|
+
rows: @rows,
|
20
|
+
resource: @resource,
|
21
|
+
resource_class: @resource_class
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ApiControllerConcerns
|
2
|
+
module ExceptionHandling
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
rescue_from Exception do |exception|
|
7
|
+
if Rails.env.development? || Rails.env.test?
|
8
|
+
error = { message: exception.message }
|
9
|
+
|
10
|
+
error[:application_trace] = Rails.backtrace_cleaner.clean(exception.backtrace)
|
11
|
+
error[:full_trace] = exception.backtrace
|
12
|
+
|
13
|
+
respond_to do |format|
|
14
|
+
format.json { render json: error, status: 500 }
|
15
|
+
end
|
16
|
+
else
|
17
|
+
respond_to do |format|
|
18
|
+
format.json { render json: { error: 'Internal server error.' }, status: 500 }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ResourcesController::Sorting
|
2
|
+
private
|
3
|
+
|
4
|
+
def load_collection_scope
|
5
|
+
add_order_scope(super)
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_order_scope(base_scope)
|
9
|
+
if params[:sort_by].present?
|
10
|
+
base_scope.order(params[:sort_by] => (params[:sort_direction] || :asc))
|
11
|
+
else
|
12
|
+
base_scope
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,254 @@
|
|
1
|
+
module Api
|
2
|
+
module ResourcesController
|
3
|
+
class Base < ::ApiController
|
4
|
+
module RestActions
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include ActionController::MimeResponds
|
9
|
+
|
10
|
+
respond_to :json
|
11
|
+
|
12
|
+
if respond_to?(:before_action)
|
13
|
+
before_action :load_collection, only: [:index]
|
14
|
+
before_action :load_resource_for_show, only: [:show]
|
15
|
+
before_action :load_resource, only: [:update, :destroy, :delete]
|
16
|
+
before_action :initialize_resource_for_create, only: [:create]
|
17
|
+
else
|
18
|
+
before_filter :load_collection, only: [:index]
|
19
|
+
before_filter :load_resource_for_show, only: [:show]
|
20
|
+
before_filter :load_resource, only: [:update, :destroy, :delete]
|
21
|
+
before_filter :initialize_resource_for_create, only: [:create]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def index
|
26
|
+
respond_to do |format|
|
27
|
+
format.json { render json: serialize_collection(@collection) }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def show
|
32
|
+
respond_to do |format|
|
33
|
+
if @resource.nil?
|
34
|
+
format.json { render json: { error: "Couldn't find #{resource_class} with ID=#{params[:id]}" }, status: :not_found }
|
35
|
+
else
|
36
|
+
format.json { render json: serialize_resource(@resource), status: :ok }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def create
|
42
|
+
respond_to do |format|
|
43
|
+
if @resource.save
|
44
|
+
format.json { render json: serialize_resource(@resource), status: :created }
|
45
|
+
else
|
46
|
+
format.json { render json: { errors: serialize_errors(@resource.errors) }, status: 422 }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def update
|
52
|
+
respond_to do |format|
|
53
|
+
if @resource.update_attributes(permitted_params)
|
54
|
+
format.json { render json: serialize_resource(@resource) }
|
55
|
+
else
|
56
|
+
format.json { render json: { errors: serialize_errors(@resource.errors) }, status: 422 }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def destroy
|
62
|
+
@resource.destroy
|
63
|
+
respond_to do |format|
|
64
|
+
format.json { render json: serialize_resource(@resource) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def delete
|
69
|
+
@resource.delete
|
70
|
+
respond_to do |format|
|
71
|
+
format.json { render json: serialize_resource(@resource) }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def load_collection
|
78
|
+
base_scope = resource_class
|
79
|
+
scope = add_conditions_from_query(base_scope)
|
80
|
+
@collection = scope.all
|
81
|
+
end
|
82
|
+
|
83
|
+
def load_resource
|
84
|
+
@resource = resource_class.find(params[:id])
|
85
|
+
end
|
86
|
+
|
87
|
+
def load_resource_for_show
|
88
|
+
begin
|
89
|
+
@resource = resource_class.find(params[:id])
|
90
|
+
rescue ActiveRecord::RecordNotFound
|
91
|
+
@resource = nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def initialize_resource
|
96
|
+
@resource = resource_class.new
|
97
|
+
end
|
98
|
+
|
99
|
+
def initialize_resource_for_create
|
100
|
+
@resource = resource_class.new(permitted_params)
|
101
|
+
end
|
102
|
+
|
103
|
+
def permitted_params
|
104
|
+
raise "not implemented"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
module QueryConditions
|
109
|
+
private
|
110
|
+
|
111
|
+
def add_conditions_from_query(scope)
|
112
|
+
request.query_parameters.each do |field, condition|
|
113
|
+
case field
|
114
|
+
when 'limit'
|
115
|
+
scope = scope.limit(condition)
|
116
|
+
when 'offset'
|
117
|
+
scope = scope.offset(condition)
|
118
|
+
when 'order'
|
119
|
+
scope = scope.order(condition)
|
120
|
+
when 'includes'
|
121
|
+
scope = scope.includes(condition.to_sym)
|
122
|
+
else
|
123
|
+
condition_statement = ::Api::ResourcesController::ConditionParser.new(field, condition).condition_statement
|
124
|
+
scope = scope.where(condition_statement)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
scope
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
include QueryConditions
|
132
|
+
|
133
|
+
module Resources
|
134
|
+
extend ActiveSupport::Concern
|
135
|
+
|
136
|
+
def resource_class
|
137
|
+
self.class.resource_class
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
module RestResourceUrls
|
142
|
+
extend ActiveSupport::Concern
|
143
|
+
|
144
|
+
private
|
145
|
+
|
146
|
+
def resource_url(resource)
|
147
|
+
url_for(action: :show, id: resource.to_param)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
module Serialization
|
152
|
+
private
|
153
|
+
|
154
|
+
def serialize_collection(collection)
|
155
|
+
collection.collect do |resource|
|
156
|
+
json = resource.as_json
|
157
|
+
json[:errors] = serialize_errors(resource.errors) if resource.errors.any?
|
158
|
+
json
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def serialize_resource(resource)
|
163
|
+
json = resource.as_json
|
164
|
+
json[:errors] = serialize_errors(resource.errors) if resource.errors.any?
|
165
|
+
json
|
166
|
+
end
|
167
|
+
|
168
|
+
def serialize_errors(errors)
|
169
|
+
errors.as_json(full_messages: true)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
module CountAction
|
174
|
+
extend ActiveSupport::Concern
|
175
|
+
|
176
|
+
included do
|
177
|
+
if respond_to?(:before_action)
|
178
|
+
before_action :load_count, only: [:count]
|
179
|
+
else
|
180
|
+
before_filter :load_count, only: [:count]
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def count
|
185
|
+
respond_to do |format|
|
186
|
+
format.json { render json: { count: @count } }
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
def load_count
|
193
|
+
@count = resource_class.count
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
module DestroyAllAction
|
198
|
+
extend ActiveSupport::Concern
|
199
|
+
|
200
|
+
included do
|
201
|
+
if respond_to?(:before_action)
|
202
|
+
before_action :load_and_destroy_collection, only: [:destroy_all]
|
203
|
+
else
|
204
|
+
before_filter :load_and_destroy_collection, only: [:destroy_all]
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def destroy_all
|
209
|
+
respond_to do |format|
|
210
|
+
format.json { render json: serialize_collection(@collection) }
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
private
|
215
|
+
|
216
|
+
def load_and_destroy_collection
|
217
|
+
@collection = resource_class.destroy_all
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
module DeleteAllAction
|
222
|
+
extend ActiveSupport::Concern
|
223
|
+
|
224
|
+
included do
|
225
|
+
if respond_to?(:before_action)
|
226
|
+
before_action :delete_collection, only: [:delete_all]
|
227
|
+
else
|
228
|
+
before_filter :delete_collection, only: [:delete_all]
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def delete_all
|
233
|
+
respond_to do |format|
|
234
|
+
format.json { render json: { count: @count } }
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
private
|
239
|
+
|
240
|
+
def delete_collection
|
241
|
+
@count = resource_class.delete_all
|
242
|
+
end
|
243
|
+
end
|
244
|
+
include RestActions
|
245
|
+
include Resources
|
246
|
+
include RestResourceUrls
|
247
|
+
include Serialization
|
248
|
+
include CountAction
|
249
|
+
include DestroyAllAction
|
250
|
+
include DeleteAllAction
|
251
|
+
include ApiControllerConcerns::ExceptionHandling
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|