rao-resources_controller 0.0.3.pre
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.md +34 -0
- data/Rakefile +25 -0
- data/app/concerns/rao/controller/autosubmit_concern.rb +50 -0
- data/app/concerns/rao/controller/query_conditions.rb +53 -0
- data/app/concerns/rao/resources_controller/acts_as_list_concern.rb +38 -0
- data/app/concerns/rao/resources_controller/acts_as_published_concern.rb +73 -0
- data/app/concerns/rao/resources_controller/awesome_nested_set_concern.rb +31 -0
- data/app/concerns/rao/resources_controller/batch_actions_concern.rb +23 -0
- data/app/concerns/rao/resources_controller/friendly_id_concern.rb +15 -0
- data/app/concerns/rao/resources_controller/kaminari_concern.rb +17 -0
- data/app/concerns/rao/resources_controller/location_history_concern.rb +42 -0
- data/app/concerns/rao/resources_controller/pagination_concern.rb +17 -0
- data/app/concerns/rao/resources_controller/resource_inflections_concern.rb +18 -0
- data/app/concerns/rao/resources_controller/resources_concern.rb +13 -0
- data/app/concerns/rao/resources_controller/rest_actions_concern.rb +93 -0
- data/app/concerns/rao/resources_controller/rest_resource_urls_concern.rb +34 -0
- data/app/concerns/rao/resources_controller/search_form_concern.rb +9 -0
- data/app/concerns/rao/resources_controller/sorting_concern.rb +28 -0
- data/app/concerns/rao/resources_controller/will_paginate_concern.rb +33 -0
- data/app/controllers/rao/resources_controller/base.rb +18 -0
- data/app/views/rao/resources_controller/base/_after_show_table.haml +0 -0
- data/app/views/rao/resources_controller/base/_before_index_table.haml +0 -0
- data/app/views/rao/resources_controller/base/_before_show_table.haml +0 -0
- data/app/views/rao/resources_controller/base/_form.haml +2 -0
- data/app/views/rao/resources_controller/base/_form_buttons.haml +5 -0
- data/app/views/rao/resources_controller/base/_form_errors.haml +6 -0
- data/app/views/rao/resources_controller/base/_pagination.haml +4 -0
- data/app/views/rao/resources_controller/base/_show.haml +2 -0
- data/app/views/rao/resources_controller/base/_show_actions.haml +11 -0
- data/app/views/rao/resources_controller/base/_table.haml +2 -0
- data/app/views/rao/resources_controller/base/_table_actions.haml +12 -0
- data/app/views/rao/resources_controller/base/edit.haml +6 -0
- data/app/views/rao/resources_controller/base/index.haml +15 -0
- data/app/views/rao/resources_controller/base/new.haml +6 -0
- data/app/views/rao/resources_controller/base/show.haml +10 -0
- data/config/locales/de.yml +28 -0
- data/config/locales/en.yml +28 -0
- data/lib/generators/rao/resources_controller/install_generator.rb +15 -0
- data/lib/generators/rao/resources_controller/templates/initializer.rb +2 -0
- data/lib/rao/resources_controller/configuration.rb +9 -0
- data/lib/rao/resources_controller/engine.rb +7 -0
- data/lib/rao/resources_controller/version.rb +7 -0
- data/lib/rao/resources_controller.rb +9 -0
- data/lib/rao-resources_controller.rb +2 -0
- data/lib/tasks/rao-resources_controller_tasks.rake +4 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4168cf6f8b9fd971130cc015e27bc69db93595a6
|
4
|
+
data.tar.gz: 858e964d860f7fd350523ab82916b86ffd69b5cb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e5ffc3de3b335935d160f1e5bd351d91949bfce6a5a8c41d8433c49ed4207d75eb3282407626b31ad184ce02a2521248a5788642dc4abc4b730a5b4eec79a5c
|
7
|
+
data.tar.gz: fd53a6823d957428e560cbce79592f7568e04958f6002625ddb85e595d8586be7cd87ebdb524f2f2b36b1a90fd8ec704eee3902b941e39613ad7f26be1472499
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 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,34 @@
|
|
1
|
+
# Rails AddOns Resources Controller
|
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 'rao-resources_controller'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install rao-resources_controller
|
22
|
+
```
|
23
|
+
|
24
|
+
Generate the initializer:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
$ rails g rao:resources_controller:install
|
28
|
+
```
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
Contribution directions go here.
|
32
|
+
|
33
|
+
## License
|
34
|
+
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,25 @@
|
|
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 Add-Ons Resources Controller'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Rao
|
2
|
+
module Controller
|
3
|
+
# Handles automatic form submits.
|
4
|
+
#
|
5
|
+
# Prerequisites:
|
6
|
+
#
|
7
|
+
# Include the javascript:
|
8
|
+
#
|
9
|
+
# # app/assets/javascripts/application.js
|
10
|
+
# //= require rao/resources_controller/application/autosubmit
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# # app/controllers/posts_controller.rb
|
15
|
+
# class PostsController < ApplicationController
|
16
|
+
# include Rao::Controller::AutosubmitConcern
|
17
|
+
#
|
18
|
+
# # ...
|
19
|
+
#
|
20
|
+
# private
|
21
|
+
#
|
22
|
+
# def autosubmit?
|
23
|
+
# true
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# # app/views/posts/new.html.haml
|
28
|
+
# = form_for(...) do |f|
|
29
|
+
# = autosubmit
|
30
|
+
#
|
31
|
+
module AutosubmitConcern
|
32
|
+
extend ActiveSupport::Concern
|
33
|
+
|
34
|
+
included do
|
35
|
+
helper Rao::AutosubmitHelper
|
36
|
+
helper_method :autosubmit?, :autosubmit_now?
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def autosubmit?
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
def autosubmit_now?
|
46
|
+
autosubmit? && action_name == 'new'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Rao
|
2
|
+
module Controller::QueryConditions
|
3
|
+
private
|
4
|
+
|
5
|
+
def add_conditions_from_query(scope)
|
6
|
+
if query_params.keys.include?('q')
|
7
|
+
condition_params = normalize_query_params(query_params)
|
8
|
+
else
|
9
|
+
condition_params = query_params
|
10
|
+
end
|
11
|
+
|
12
|
+
condition_params.reject.reject { |k,v| v.blank? }.each do |field, condition|
|
13
|
+
case field
|
14
|
+
when 'limit'
|
15
|
+
scope = scope.limit(condition.to_i)
|
16
|
+
when 'offset'
|
17
|
+
scope = scope.offset(condition.to_i)
|
18
|
+
when 'order'
|
19
|
+
scope = scope.order(condition)
|
20
|
+
when 'includes'
|
21
|
+
scope = scope.includes(condition.map(&:to_sym))
|
22
|
+
else
|
23
|
+
condition_statement = ::Api::ResourcesController::ConditionParser.new(scope, field, condition).condition_statement
|
24
|
+
scope = scope.where(condition_statement)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
scope
|
28
|
+
end
|
29
|
+
|
30
|
+
def query_params
|
31
|
+
default_query_params
|
32
|
+
end
|
33
|
+
|
34
|
+
def default_query_params
|
35
|
+
request.query_parameters.except(*default_query_params_exceptions)
|
36
|
+
end
|
37
|
+
|
38
|
+
def default_query_params_exceptions
|
39
|
+
%w(sort_by sort_direction utf8 commit page)
|
40
|
+
end
|
41
|
+
|
42
|
+
def normalize_query_params(params)
|
43
|
+
params['q'].each_with_object({}) { |(k, v), m| m[normalize_key(k)] = v }
|
44
|
+
end
|
45
|
+
|
46
|
+
def normalize_key(key)
|
47
|
+
splitted_key = key.split('_')
|
48
|
+
predicate = splitted_key.last
|
49
|
+
attribute = splitted_key[0..-2].join('_')
|
50
|
+
"#{attribute}(#{predicate})"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::ActsAsListConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def reposition
|
6
|
+
@resource = load_resource
|
7
|
+
@dropped_resource = load_resource_scope.find(params[:dropped_id])
|
8
|
+
@dropped_resource.set_list_position(@resource.position)
|
9
|
+
position = @dropped_resource.position < @resource.position ? :before : :after
|
10
|
+
|
11
|
+
label_methods = [:human, :name, :title, :email, :to_s]
|
12
|
+
|
13
|
+
target_resource_label = nil
|
14
|
+
label_methods.each do |method_name|
|
15
|
+
if @resource.respond_to?(method_name)
|
16
|
+
target_resource_label = @resource.send(method_name)
|
17
|
+
break
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
inserted_resource_label = nil
|
22
|
+
label_methods.each do |method_name|
|
23
|
+
if @dropped_resource.respond_to?(method_name)
|
24
|
+
inserted_resource_label = @dropped_resource.send(method_name)
|
25
|
+
break
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
redirect_to after_reposition_location, notice: I18n.t("acts_as_list.flash.actions.reposition.inserted_#{position}", target_resource: target_resource_label, inserted_resource: inserted_resource_label)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def after_reposition_location
|
35
|
+
collection_path
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::ActsAsPublishedConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def publish_many
|
6
|
+
@collection = load_collection_scope.find(params[:ids])
|
7
|
+
@collection.map(&:publish!)
|
8
|
+
|
9
|
+
resource_labels = []
|
10
|
+
@collection.each do |resource|
|
11
|
+
[:human, :name, :title, :email, :to_s].each do |method_name|
|
12
|
+
if resource.respond_to?(method_name)
|
13
|
+
resource_labels << resource.send(method_name)
|
14
|
+
break
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
notice = I18n.t("acts_as_published.notices.published_many", names: resource_labels.to_sentence)
|
20
|
+
if Rails.version < '5.0.0'
|
21
|
+
redirect_to :back, notice: notice
|
22
|
+
else
|
23
|
+
flash[:notice] = notice
|
24
|
+
redirect_back(fallback_location: main_app.root_path)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def unpublish_many
|
29
|
+
@collection = load_collection_scope.find(params[:ids])
|
30
|
+
@collection.map(&:unpublish!)
|
31
|
+
|
32
|
+
resource_labels = []
|
33
|
+
@collection.each do |resource|
|
34
|
+
[:human, :name, :title, :email, :to_s].each do |method_name|
|
35
|
+
if resource.respond_to?(method_name)
|
36
|
+
resource_labels << resource.send(method_name)
|
37
|
+
break
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
notice = I18n.t("acts_as_published.notices.unpublished_many", names: resource_labels.to_sentence)
|
43
|
+
if Rails.version < '5.0.0'
|
44
|
+
redirect_to :back, notice: notice
|
45
|
+
else
|
46
|
+
flash[:notice] = notice
|
47
|
+
redirect_back(fallback_location: main_app.root_path)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def toggle_published
|
52
|
+
@resource = load_resource
|
53
|
+
@resource.toggle_published!
|
54
|
+
|
55
|
+
action_taken = @resource.published? ? 'published' : 'unpublished'
|
56
|
+
|
57
|
+
resource_label = nil
|
58
|
+
[:human, :name, :title, :email, :to_s].each do |method_name|
|
59
|
+
if @resource.respond_to?(method_name)
|
60
|
+
resource_label = @resource.send(method_name)
|
61
|
+
break
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
if Rails.version < '5.0.0'
|
66
|
+
redirect_to :back, notice: I18n.t("acts_as_published.notices.#{action_taken}", name: resource_label)
|
67
|
+
else
|
68
|
+
flash[:notice] = I18n.t("acts_as_published.notices.#{action_taken}", name: resource_label)
|
69
|
+
redirect_back(fallback_location: main_app.root_path)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::AwesomeNestedSetConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def reposition
|
6
|
+
@resource = load_resource
|
7
|
+
@dropped_resource = resource_class.find(params[:dropped_id])
|
8
|
+
@dropped_resource.move_to_right_of(@resource)
|
9
|
+
|
10
|
+
label_methods = [:human, :name, :email, :to_s]
|
11
|
+
|
12
|
+
target_resource_label = nil
|
13
|
+
label_methods.each do |method_name|
|
14
|
+
if @resource.respond_to?(method_name)
|
15
|
+
target_resource_label = @resource.send(method_name)
|
16
|
+
break
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
inserted_resource_label = nil
|
21
|
+
label_methods.each do |method_name|
|
22
|
+
if @dropped_resource.respond_to?(method_name)
|
23
|
+
inserted_resource_label = @dropped_resource.send(method_name)
|
24
|
+
break
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
redirect_to collection_path, notice: I18n.t("awesome_nested_set.flash.actions.reposition.notice", target_resource: target_resource_label, inserted_resource: inserted_resource_label)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Rao
|
2
|
+
# Don't forget to add routes:
|
3
|
+
#
|
4
|
+
# # config/routes.rb;
|
5
|
+
# resources :users do
|
6
|
+
# post :destroy_many, on: :collection
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
module ResourcesController::BatchActionsConcern
|
10
|
+
def destroy_many
|
11
|
+
@collection = load_collection_scope.where(id: params[:ids])
|
12
|
+
@collection.destroy_all
|
13
|
+
|
14
|
+
respond_with @collection, location: after_destroy_many_location, notice: t('.success', count: @collection.count)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def after_destroy_many_location
|
20
|
+
collection_path
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::KaminariConcern
|
3
|
+
def self.included(base)
|
4
|
+
base.helper_method :paginate?
|
5
|
+
end
|
6
|
+
|
7
|
+
def paginate?
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def load_collection
|
14
|
+
@collection = load_collection_scope.page(params[:page])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::LocationHistoryConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
if respond_to?(:before_action)
|
7
|
+
before_action :store_location
|
8
|
+
else
|
9
|
+
before_filter :store_location
|
10
|
+
end
|
11
|
+
|
12
|
+
helper_method :last_location
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def store_location
|
18
|
+
return if request.referer.nil?
|
19
|
+
truncate_location_history(9)
|
20
|
+
puts "[LocationHistoryConcern] Storing last location [#{request.referer}]"
|
21
|
+
location_history[Time.zone.now] = request.referer
|
22
|
+
end
|
23
|
+
|
24
|
+
def location_history
|
25
|
+
session[:location_history] ||= {}
|
26
|
+
end
|
27
|
+
|
28
|
+
def last_location
|
29
|
+
location_history.sort.last.try(:last)
|
30
|
+
end
|
31
|
+
|
32
|
+
def truncate_location_history(count = 0)
|
33
|
+
return if location_history.size <= count
|
34
|
+
truncated = session[:location_history].sort.last(count)
|
35
|
+
session[:location_history] = if truncated.respond_to?(:to_h)
|
36
|
+
truncated.to_h
|
37
|
+
else
|
38
|
+
truncated.each_with_object({}) { |a, hash| hash[a.first] = a.last }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::PaginationConcern
|
3
|
+
def self.included(base)
|
4
|
+
base.helper_method :paginate?
|
5
|
+
end
|
6
|
+
|
7
|
+
def paginate?
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def load_collection
|
14
|
+
@collection = load_collection_scope.page(params[:page])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::ResourceInflectionsConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :inflections
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def inflections
|
12
|
+
{
|
13
|
+
resource_name: resource_class.model_name.human(count: 1),
|
14
|
+
collection_name: resource_class.model_name.human(count: 2)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::RestActionsConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
include ActionController::MimeResponds
|
7
|
+
|
8
|
+
respond_to :html
|
9
|
+
responders :flash
|
10
|
+
|
11
|
+
if respond_to?(:before_action)
|
12
|
+
before_action :load_collection, only: [:index]
|
13
|
+
before_action :load_resource, only: [:show, :edit, :destroy, :update]
|
14
|
+
before_action :initialize_resource, only: [:new]
|
15
|
+
before_action :initialize_resource_for_create, only: [:create]
|
16
|
+
before_action :before_rest_action, if: -> { respond_to?(:before_rest_action, true) }
|
17
|
+
else
|
18
|
+
before_filter :load_collection, only: [:index]
|
19
|
+
before_filter :load_resource, only: [:show, :edit, :destroy, :update]
|
20
|
+
before_filter :initialize_resource, only: [:new]
|
21
|
+
before_filter :initialize_resource_for_create, only: [:create]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def index; end
|
26
|
+
def new; end
|
27
|
+
def show; end
|
28
|
+
def edit; end
|
29
|
+
|
30
|
+
def update
|
31
|
+
if @resource.send(update_method_name, permitted_params) && respond_to?(:after_update_location, true) && after_update_location.present?
|
32
|
+
respond_with(respond_with_namespace, @resource, location: after_update_location)
|
33
|
+
else
|
34
|
+
respond_with(respond_with_namespace, @resource)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def destroy
|
39
|
+
@resource.destroy
|
40
|
+
if respond_to?(:after_destroy_location, true) && after_destroy_location.present?
|
41
|
+
respond_with(respond_with_namespace, @resource, location: after_destroy_location)
|
42
|
+
else
|
43
|
+
respond_with(respond_with_namespace, @resource)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def create
|
48
|
+
if @resource.save && respond_to?(:after_create_location, true) && after_create_location.present?
|
49
|
+
respond_with(respond_with_namespace, @resource, location: after_create_location)
|
50
|
+
else
|
51
|
+
respond_with(respond_with_namespace, @resource)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def update_method_name
|
58
|
+
Rails::VERSION::MAJOR < 4 ? :update_attributes : :update
|
59
|
+
end
|
60
|
+
|
61
|
+
def respond_with_namespace
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_collection_scope
|
66
|
+
resource_class
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_collection
|
70
|
+
@collection = load_collection_scope.all
|
71
|
+
end
|
72
|
+
|
73
|
+
def load_resource_scope
|
74
|
+
resource_class
|
75
|
+
end
|
76
|
+
|
77
|
+
def load_resource
|
78
|
+
@resource = load_resource_scope.find(params[:id])
|
79
|
+
end
|
80
|
+
|
81
|
+
def initialize_resource
|
82
|
+
@resource = resource_class.new
|
83
|
+
end
|
84
|
+
|
85
|
+
def initialize_resource_for_create
|
86
|
+
@resource = resource_class.new(permitted_params)
|
87
|
+
end
|
88
|
+
|
89
|
+
def permitted_params
|
90
|
+
raise "not implemented"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::RestResourceUrlsConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :new_resource_path
|
7
|
+
helper_method :collection_path
|
8
|
+
helper_method :resource_path
|
9
|
+
helper_method :edit_resource_path
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def new_resource_path
|
15
|
+
resource_router.send(:url_for, { action: :new, only_path: true })
|
16
|
+
end
|
17
|
+
|
18
|
+
def collection_path
|
19
|
+
resource_router.send(:url_for, { action: :index, only_path: true })
|
20
|
+
end
|
21
|
+
|
22
|
+
def resource_path(resource)
|
23
|
+
resource_router.send(:url_for, { action: :show, id: resource, only_path: true })
|
24
|
+
end
|
25
|
+
|
26
|
+
def edit_resource_path(resource)
|
27
|
+
resource_router.send(:url_for, { action: :edit, id: resource, only_path: true })
|
28
|
+
end
|
29
|
+
|
30
|
+
def resource_router
|
31
|
+
self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController::SortingConcern
|
3
|
+
private
|
4
|
+
|
5
|
+
def load_collection_scope
|
6
|
+
add_order_scope(super)
|
7
|
+
end
|
8
|
+
|
9
|
+
def add_order_scope(base_scope)
|
10
|
+
if params[:sort_by].present?
|
11
|
+
if params[:sort_by].include?(' ') || params[:sort_direction].include?(' ')
|
12
|
+
raise "Possible SQL Injection attempt while trying to sort by #{params[:sort_by]} #{params[:sort_direction]}"
|
13
|
+
end
|
14
|
+
|
15
|
+
sort_by = params[:sort_by]
|
16
|
+
sort_direction = (params[:sort_direction] || :asc)
|
17
|
+
|
18
|
+
if sort_by.include?('.')
|
19
|
+
base_scope.reorder("#{sort_by} #{sort_direction}")
|
20
|
+
else
|
21
|
+
base_scope.reorder(sort_by => sort_direction)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
base_scope
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Rao
|
2
|
+
# Setting the pagination size in the controller:
|
3
|
+
#
|
4
|
+
# # app/controllers/posts_controller.rb
|
5
|
+
# class PostsController < ApplicationController
|
6
|
+
# # ...
|
7
|
+
# private
|
8
|
+
#
|
9
|
+
# def per_page
|
10
|
+
# 15
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
module ResourcesController::WillPaginateConcern
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
|
17
|
+
included do
|
18
|
+
helper_method :paginate?
|
19
|
+
end
|
20
|
+
|
21
|
+
def paginate?
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def load_collection
|
28
|
+
options = { page: params[:page] }
|
29
|
+
options[:per_page] = per_page if respond_to?(:per_page, true)
|
30
|
+
@collection = load_collection_scope.paginate(options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController
|
3
|
+
class Base < Rails::AddOns::Configuration.resources_controller_base_class_name.constantize
|
4
|
+
include RestActionsConcern
|
5
|
+
include ResourcesConcern
|
6
|
+
include RestResourceUrlsConcern
|
7
|
+
include ResourceInflectionsConcern
|
8
|
+
include LocationHistoryConcern
|
9
|
+
include ::Rao::Controller::QueryConditionsConcern
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def load_collection_scope
|
14
|
+
add_conditions_from_query(resource_class)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,5 @@
|
|
1
|
+
= form.button :submit, class: 'btn btn-success' do
|
2
|
+
= fa_icon :check
|
3
|
+
= t('.submit', model_name: form.object.model_name.human)
|
4
|
+
= bootstrap_button(to: ((action_name == 'new' || form.object.new_record?) ? collection_path : resource_path(form.object)), context: :link) do
|
5
|
+
= t('.cancel')
|
@@ -0,0 +1,6 @@
|
|
1
|
+
- if resource.errors.any?
|
2
|
+
= bootstrap_alert(context: :danger, class: 'error-explanation') do
|
3
|
+
.error-heading= t('errors.template.header', count: resource.errors.count, model: resource.class.model_name.human)
|
4
|
+
%ul
|
5
|
+
- resource.errors.full_messages.each do |msg|
|
6
|
+
%li= msg
|
@@ -0,0 +1,11 @@
|
|
1
|
+
= bootstrap_button_group do
|
2
|
+
= bootstrap_button(to: edit_resource_path(resource), context: :secondary) do
|
3
|
+
= fa_icon(:pencil)
|
4
|
+
= t('.edit')
|
5
|
+
|
6
|
+
= bootstrap_button(to: resource_path(resource), context: :danger, method: :delete, data: { confirm: 'Are you sure?' }) do
|
7
|
+
= fa_icon :trash
|
8
|
+
= t('.delete')
|
9
|
+
|
10
|
+
= bootstrap_button(to: collection_path, context: :link) do
|
11
|
+
= t('.back')
|
@@ -0,0 +1,12 @@
|
|
1
|
+
= table.column :actions do |resource|
|
2
|
+
- capture_haml do
|
3
|
+
= bootstrap_button_group do
|
4
|
+
= bootstrap_button(to: resource_path(resource), context: :primary, size: :small) do
|
5
|
+
= fa_icon :eye
|
6
|
+
= t('.show')
|
7
|
+
= bootstrap_button(to: edit_resource_path(resource), context: :secondary, size: :small) do
|
8
|
+
= fa_icon :pencil
|
9
|
+
= t('.edit')
|
10
|
+
= bootstrap_button(to: resource_path(resource), context: :danger, size: :small, method: :delete, data: { confirm: 'Are you sure?' }) do
|
11
|
+
= fa_icon :trash
|
12
|
+
= t('.delete')
|
@@ -0,0 +1,15 @@
|
|
1
|
+
%h1
|
2
|
+
%span
|
3
|
+
= resource_class.model_name.human(count: :other)
|
4
|
+
.pull-right
|
5
|
+
= bootstrap_button(to: new_resource_path, context: :success) do
|
6
|
+
= fa_icon(:plus)
|
7
|
+
= t('.new')
|
8
|
+
|
9
|
+
= render 'before_index_table', collection: @collection
|
10
|
+
|
11
|
+
= collection_table(collection: @collection, resource_class: resource_class) do |t|
|
12
|
+
= render 'table', table: t
|
13
|
+
= render 'table_actions', table: t
|
14
|
+
|
15
|
+
= render partial: 'pagination', locals: { collection: @collection} if respond_to?(:paginate?) && paginate?
|
@@ -0,0 +1,10 @@
|
|
1
|
+
%h1= @resource.try_all(:human, :title, :name, :to_s)
|
2
|
+
|
3
|
+
= render 'before_show_table', resource: @resource
|
4
|
+
|
5
|
+
= resource_table(resource: @resource) do |t|
|
6
|
+
= render 'show', table: t
|
7
|
+
|
8
|
+
= render 'after_show_table', resource: @resource
|
9
|
+
|
10
|
+
= render 'show_actions', resource: @resource
|
@@ -0,0 +1,28 @@
|
|
1
|
+
de:
|
2
|
+
flash:
|
3
|
+
actions:
|
4
|
+
create:
|
5
|
+
notice: "%{resource_name} wurde erstellt."
|
6
|
+
update:
|
7
|
+
notice: "%{resource_name} wurde aktualisiert."
|
8
|
+
destroy:
|
9
|
+
notice: "%{resource_name} wurde gelöscht."
|
10
|
+
alert: "%{resource_name} konnte nicht gelöscht werden."
|
11
|
+
perform:
|
12
|
+
notice: "%{resource_name} wurde ausgeführt."
|
13
|
+
alert: "%{resource_name} konnte nicht ausgeführt werden. Es traten folgende Fehler auf: %{errors}"
|
14
|
+
rao:
|
15
|
+
resources_controller:
|
16
|
+
base:
|
17
|
+
form_buttons:
|
18
|
+
cancel: "Abbrechen"
|
19
|
+
edit:
|
20
|
+
title: "%{resource_name} bearbeiten"
|
21
|
+
index:
|
22
|
+
new: "Erstellen"
|
23
|
+
new:
|
24
|
+
title: "%{resource_name} erstellen"
|
25
|
+
show_actions:
|
26
|
+
back: "Zurück"
|
27
|
+
delete: "Löschen"
|
28
|
+
edit: "Bearbeiten"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
en:
|
2
|
+
flash:
|
3
|
+
actions:
|
4
|
+
create:
|
5
|
+
notice: "%{resource_name} was successfully created."
|
6
|
+
update:
|
7
|
+
notice: "%{resource_name} was successfully updated."
|
8
|
+
destroy:
|
9
|
+
notice: "%{resource_name} was successfully destroyed."
|
10
|
+
alert: "%{resource_name} could not be destroyed."
|
11
|
+
perform:
|
12
|
+
notice: "%{resource_name} was executed."
|
13
|
+
alert: "%{resource_name} could not be executed. Errors: %{errors}"
|
14
|
+
rao:
|
15
|
+
resources_controller:
|
16
|
+
base:
|
17
|
+
form_buttons:
|
18
|
+
cancel: "Cancel"
|
19
|
+
edit:
|
20
|
+
title: "Edit %{resource_name}"
|
21
|
+
index:
|
22
|
+
new: "New"
|
23
|
+
new:
|
24
|
+
title: "New %{resource_name}"
|
25
|
+
show_actions:
|
26
|
+
back: "Back"
|
27
|
+
delete: "Delete"
|
28
|
+
edit: "Edit"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Rao
|
2
|
+
module ResourcesController
|
3
|
+
module Generators
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
desc 'Generates the initializer'
|
6
|
+
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
def generate_initializer
|
10
|
+
template 'initializer.rb', 'config/initializers/rao-resources_controller.rb'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rao-resources_controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roberto Vasquez Angel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rao
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- roberto@vasquez-angel.de
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- app/concerns/rao/controller/autosubmit_concern.rb
|
94
|
+
- app/concerns/rao/controller/query_conditions.rb
|
95
|
+
- app/concerns/rao/resources_controller/acts_as_list_concern.rb
|
96
|
+
- app/concerns/rao/resources_controller/acts_as_published_concern.rb
|
97
|
+
- app/concerns/rao/resources_controller/awesome_nested_set_concern.rb
|
98
|
+
- app/concerns/rao/resources_controller/batch_actions_concern.rb
|
99
|
+
- app/concerns/rao/resources_controller/friendly_id_concern.rb
|
100
|
+
- app/concerns/rao/resources_controller/kaminari_concern.rb
|
101
|
+
- app/concerns/rao/resources_controller/location_history_concern.rb
|
102
|
+
- app/concerns/rao/resources_controller/pagination_concern.rb
|
103
|
+
- app/concerns/rao/resources_controller/resource_inflections_concern.rb
|
104
|
+
- app/concerns/rao/resources_controller/resources_concern.rb
|
105
|
+
- app/concerns/rao/resources_controller/rest_actions_concern.rb
|
106
|
+
- app/concerns/rao/resources_controller/rest_resource_urls_concern.rb
|
107
|
+
- app/concerns/rao/resources_controller/search_form_concern.rb
|
108
|
+
- app/concerns/rao/resources_controller/sorting_concern.rb
|
109
|
+
- app/concerns/rao/resources_controller/will_paginate_concern.rb
|
110
|
+
- app/controllers/rao/resources_controller/base.rb
|
111
|
+
- app/views/rao/resources_controller/base/_after_show_table.haml
|
112
|
+
- app/views/rao/resources_controller/base/_before_index_table.haml
|
113
|
+
- app/views/rao/resources_controller/base/_before_show_table.haml
|
114
|
+
- app/views/rao/resources_controller/base/_form.haml
|
115
|
+
- app/views/rao/resources_controller/base/_form_buttons.haml
|
116
|
+
- app/views/rao/resources_controller/base/_form_errors.haml
|
117
|
+
- app/views/rao/resources_controller/base/_pagination.haml
|
118
|
+
- app/views/rao/resources_controller/base/_show.haml
|
119
|
+
- app/views/rao/resources_controller/base/_show_actions.haml
|
120
|
+
- app/views/rao/resources_controller/base/_table.haml
|
121
|
+
- app/views/rao/resources_controller/base/_table_actions.haml
|
122
|
+
- app/views/rao/resources_controller/base/edit.haml
|
123
|
+
- app/views/rao/resources_controller/base/index.haml
|
124
|
+
- app/views/rao/resources_controller/base/new.haml
|
125
|
+
- app/views/rao/resources_controller/base/show.haml
|
126
|
+
- config/locales/de.yml
|
127
|
+
- config/locales/en.yml
|
128
|
+
- lib/generators/rao/resources_controller/install_generator.rb
|
129
|
+
- lib/generators/rao/resources_controller/templates/initializer.rb
|
130
|
+
- lib/rao-resources_controller.rb
|
131
|
+
- lib/rao/resources_controller.rb
|
132
|
+
- lib/rao/resources_controller/configuration.rb
|
133
|
+
- lib/rao/resources_controller/engine.rb
|
134
|
+
- lib/rao/resources_controller/version.rb
|
135
|
+
- lib/tasks/rao-resources_controller_tasks.rake
|
136
|
+
homepage: https://github.com/rails-add_ons
|
137
|
+
licenses:
|
138
|
+
- MIT
|
139
|
+
metadata: {}
|
140
|
+
post_install_message:
|
141
|
+
rdoc_options: []
|
142
|
+
require_paths:
|
143
|
+
- lib
|
144
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">"
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: 1.3.1
|
154
|
+
requirements: []
|
155
|
+
rubyforge_project:
|
156
|
+
rubygems_version: 2.6.14
|
157
|
+
signing_key:
|
158
|
+
specification_version: 4
|
159
|
+
summary: Resources Controllers for Ruby on Rails.
|
160
|
+
test_files: []
|