action_crud 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +64 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +10 -0
- data/lib/action_crud.rb +28 -0
- data/lib/action_crud/controller.rb +174 -0
- data/lib/action_crud/helpers/link.rb +54 -0
- data/lib/action_crud/helpers/route.rb +53 -0
- data/lib/action_crud/helpers/url.rb +69 -0
- data/lib/action_crud/helpers/view.rb +44 -0
- data/lib/action_crud/pagination.rb +57 -0
- data/lib/action_crud/version.rb +3 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 239ccd1fb5a941484a62505a460d63a908ef14d2
|
4
|
+
data.tar.gz: 71a5a0f71c1b84af387dfc2183daf83cd9847f53
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dc062dc715d3ffaac8504f24ef5a9317b80c11401cac64ad6d34f5b152d371c37ddc9e2d68dd85ac71dd17e0d9fcf6a791edfa5cea6f8d0e55cea744c63fe114
|
7
|
+
data.tar.gz: 779ce72b7c8f914ff9ca673f6ae1e2f251d0ceb3e4e59f132a0cf91d8253b16dd3f77083acf3d75239238b89d1befe424b3aa512c4433afaf79ab875e493d128
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
action_crud (0.1.0)
|
5
|
+
actionpack (~> 5.0)
|
6
|
+
i18n (~> 0.8)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (5.1.4)
|
12
|
+
actionview (= 5.1.4)
|
13
|
+
activesupport (= 5.1.4)
|
14
|
+
rack (~> 2.0)
|
15
|
+
rack-test (>= 0.6.3)
|
16
|
+
rails-dom-testing (~> 2.0)
|
17
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
18
|
+
actionview (5.1.4)
|
19
|
+
activesupport (= 5.1.4)
|
20
|
+
builder (~> 3.1)
|
21
|
+
erubi (~> 1.4)
|
22
|
+
rails-dom-testing (~> 2.0)
|
23
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
24
|
+
activesupport (5.1.4)
|
25
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
26
|
+
i18n (~> 0.7)
|
27
|
+
minitest (~> 5.1)
|
28
|
+
tzinfo (~> 1.1)
|
29
|
+
builder (3.2.3)
|
30
|
+
concurrent-ruby (1.0.5)
|
31
|
+
crass (1.0.2)
|
32
|
+
erubi (1.6.1)
|
33
|
+
i18n (0.8.6)
|
34
|
+
loofah (2.1.1)
|
35
|
+
crass (~> 1.0.2)
|
36
|
+
nokogiri (>= 1.5.9)
|
37
|
+
mini_portile2 (2.3.0)
|
38
|
+
minitest (5.10.3)
|
39
|
+
nokogiri (1.8.1)
|
40
|
+
mini_portile2 (~> 2.3.0)
|
41
|
+
rack (2.0.3)
|
42
|
+
rack-test (0.7.0)
|
43
|
+
rack (>= 1.0, < 3)
|
44
|
+
rails-dom-testing (2.0.3)
|
45
|
+
activesupport (>= 4.2.0)
|
46
|
+
nokogiri (>= 1.6)
|
47
|
+
rails-html-sanitizer (1.0.3)
|
48
|
+
loofah (~> 2.0)
|
49
|
+
rake (10.5.0)
|
50
|
+
thread_safe (0.3.6)
|
51
|
+
tzinfo (1.2.3)
|
52
|
+
thread_safe (~> 0.1)
|
53
|
+
|
54
|
+
PLATFORMS
|
55
|
+
ruby
|
56
|
+
|
57
|
+
DEPENDENCIES
|
58
|
+
action_crud!
|
59
|
+
bundler (~> 1.14)
|
60
|
+
minitest (~> 5.0)
|
61
|
+
rake (~> 10.0)
|
62
|
+
|
63
|
+
BUNDLED WITH
|
64
|
+
1.15.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jonian Guveli
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# ActionCrud
|
2
|
+
|
3
|
+
ActionCrud speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important. It makes your controllers more powerful and cleaner at the same time. In addition to making your controllers follow a pattern, it helps you to write better code by following fat models and skinny controllers convention.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/action_crud.svg)](https://badge.fury.io/rb/action_crud)
|
6
|
+
[![Build Status](https://travis-ci.org/hardpixel/action-crud.svg?branch=master)](https://travis-ci.org/hardpixel/action-crud)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/hardpixel/action-crud/badges/gpa.png)](https://codeclimate.com/github/hardpixel/aaction-crud)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'action_crud'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install action_crud
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To enable CRUD actions in an ActionController controller, include the `ActionCrud` concern in your class:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Post < ActionController::Base
|
31
|
+
include ActionCrud
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
To set the controller model, use the `set_model_name` function:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class Post < ActionController::Base
|
39
|
+
# Using the set_model_name function
|
40
|
+
set_model_name 'Post'
|
41
|
+
|
42
|
+
# Or by setting the model_name class attribute
|
43
|
+
self.model_name = 'Post'
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
To set a scope for the index action, use the `set_index_scope` function:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class Post < ActionController::Base
|
51
|
+
# Using the set_index_scope function
|
52
|
+
set_index_scope :published
|
53
|
+
|
54
|
+
# Or by setting the model_name class attribute
|
55
|
+
self.index_scope = :published
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
To set the permitted parameters for the controller, use the `permit_params` function. The function accepts the options `only`, `except`, `also`, `array`, `hash`. Array and hash options are used to indicate array and hash parameters. If you call the function without options it will permit all the model attribute names except `id`:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class Post < ActionController::Base
|
63
|
+
# Using the permit_params function
|
64
|
+
permit_params only: [:title, :content], array: [:categories, :tags]
|
65
|
+
|
66
|
+
# Or by setting the permitted_params class attribute
|
67
|
+
self.permitted_params = [:title, :content, [categories: []], [tags: []]]
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
# Or by overriding the record_params function
|
72
|
+
def record_params
|
73
|
+
params.require(:post).permit(:title, :content, [categories: []], [tags: []])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
After setting up the controller, like the examples above, you will have a fully working CRUD controller with instance variables `@post` and `@posts` available. Also, if you use a pagination gem like [SmartPagination](https://github.com/hardpixel/smart-pagination), the index records will be automagically paginated!
|
79
|
+
|
80
|
+
ActionCrud also injects in your views and controllers the following helpers:
|
81
|
+
|
82
|
+
| Paths | URLs | Data | Tags (views only) |
|
83
|
+
| :----------------- | :--------------- | :----------------- | :---------------- |
|
84
|
+
| `records_path` | `records_url` | `current_model` | `record_link_to` |
|
85
|
+
| `record_path` | `record_url` | `current_record` | `record_links_to` |
|
86
|
+
| `new_record_path` | `new_record_url` | `current_records` | |
|
87
|
+
| `edit_record_path` | `edit_record_url`| `permitted_params` | |
|
88
|
+
|
89
|
+
## Development
|
90
|
+
|
91
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/action-crud.
|
96
|
+
|
97
|
+
## License
|
98
|
+
|
99
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/action_crud.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'action_crud/controller'
|
2
|
+
require 'action_crud/pagination'
|
3
|
+
require 'action_crud/helpers/route'
|
4
|
+
require 'action_crud/helpers/link'
|
5
|
+
require 'action_crud/helpers/url'
|
6
|
+
require 'action_crud/helpers/view'
|
7
|
+
require 'action_crud/version'
|
8
|
+
|
9
|
+
module ActionCrud
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
# Include submodules
|
14
|
+
include ActionCrud::Controller
|
15
|
+
include ActionCrud::Pagination
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Include action view helpers
|
20
|
+
if defined? ActionView::Base
|
21
|
+
ActionView::Base.send :include, ActionCrud::Helpers::Url
|
22
|
+
ActionView::Base.send :include, ActionCrud::Helpers::View
|
23
|
+
end
|
24
|
+
|
25
|
+
# Include action controller helpers
|
26
|
+
if defined? ActionController::Base
|
27
|
+
ActionController::Base.send :include, ActionCrud::Helpers::Url
|
28
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
module ActionCrud
|
2
|
+
module Controller
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
# Class attributes
|
7
|
+
class_attribute :model_name, instance_predicate: false
|
8
|
+
class_attribute :index_scope, instance_predicate: false
|
9
|
+
class_attribute :permitted_params, instance_predicate: false
|
10
|
+
|
11
|
+
# Class attributes defaults
|
12
|
+
self.model_name = self.controller_name
|
13
|
+
self.index_scope = :all
|
14
|
+
self.permitted_params = []
|
15
|
+
|
16
|
+
# Action callbacks
|
17
|
+
before_action :set_record, only: [:show, :edit, :update, :destroy]
|
18
|
+
end
|
19
|
+
|
20
|
+
class_methods do
|
21
|
+
# Set permitted parameters
|
22
|
+
def permit_params(options={})
|
23
|
+
model = self.model_name.classify.constantize
|
24
|
+
default = { only: model.attribute_names, except: [], also: [], array: [], hash: [] }
|
25
|
+
options = Hash[default.merge(options).map { |k, v| [k, Array(v).map(&:to_sym)] }]
|
26
|
+
permit = options.except(:except).values.flatten.uniq
|
27
|
+
|
28
|
+
permit.reject! { |a| a.blank? || a.in?(options[:except] + [:id]) }
|
29
|
+
permit.map! { |a| a.in?(options[:array]) ? [a => []] : a }
|
30
|
+
permit.map! { |a| a.in?(options[:hash]) ? [a => {}] : a }
|
31
|
+
|
32
|
+
self.permitted_params = permit
|
33
|
+
end
|
34
|
+
|
35
|
+
# Set model name
|
36
|
+
def set_model_name(name)
|
37
|
+
self.model_name = name
|
38
|
+
end
|
39
|
+
|
40
|
+
# Set index scope
|
41
|
+
def set_index_scope(scope)
|
42
|
+
self.index_scope = scope
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# GET /model
|
47
|
+
def index
|
48
|
+
self.records = model.send self.index_scope
|
49
|
+
self.records = paginate(records) if respond_to? :per_page
|
50
|
+
|
51
|
+
respond_to do |format|
|
52
|
+
format.html { render :index }
|
53
|
+
format.json { render json: records }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# GET /model/new
|
58
|
+
def new
|
59
|
+
self.record = model.new
|
60
|
+
|
61
|
+
respond_to do |format|
|
62
|
+
format.html { render :new }
|
63
|
+
format.json { render json: record }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# GET /model/1
|
68
|
+
def show
|
69
|
+
respond_to do |format|
|
70
|
+
format.html { render :show }
|
71
|
+
format.json { render json: record }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# GET /model/1/edit
|
76
|
+
def edit
|
77
|
+
respond_to do |format|
|
78
|
+
format.html { render :edit }
|
79
|
+
format.json { render json: record }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# POST /model
|
84
|
+
def create
|
85
|
+
self.record = model.new(record_params)
|
86
|
+
|
87
|
+
respond_to do |format|
|
88
|
+
if record.save
|
89
|
+
format.html { redirect_to edit_record_path(record), notice: "#{model} was successfully created." }
|
90
|
+
format.json { render json: record, status: :created }
|
91
|
+
else
|
92
|
+
format.html { render :new }
|
93
|
+
format.json { render json: record.errors, status: :unprocessable_entity }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# PATCH/PUT /model/1
|
99
|
+
def update
|
100
|
+
respond_to do |format|
|
101
|
+
if record.update(record_params)
|
102
|
+
format.html { redirect_to edit_record_path(record), notice: "#{model} was successfully updated." }
|
103
|
+
format.json { render json: record, status: :ok }
|
104
|
+
else
|
105
|
+
format.html { render :edit }
|
106
|
+
format.json { render json: record.errors, status: :unprocessable_entity }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# DELETE /model/1
|
112
|
+
def destroy
|
113
|
+
record.destroy
|
114
|
+
|
115
|
+
respond_to do |format|
|
116
|
+
format.html { redirect_to records_path, notice: "#{model} was successfully destroyed." }
|
117
|
+
format.json { head :no_content }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Get model
|
122
|
+
def model
|
123
|
+
model_name.classify.constantize
|
124
|
+
end
|
125
|
+
|
126
|
+
alias :current_model :model
|
127
|
+
|
128
|
+
# Get single record
|
129
|
+
def record
|
130
|
+
instance_variable_get "@#{singular_name}"
|
131
|
+
end
|
132
|
+
|
133
|
+
alias :current_record :record
|
134
|
+
|
135
|
+
# Get records collection
|
136
|
+
def records
|
137
|
+
instance_variable_get "@#{plural_name}"
|
138
|
+
end
|
139
|
+
|
140
|
+
alias :current_records :records
|
141
|
+
|
142
|
+
private
|
143
|
+
|
144
|
+
# Get singular name
|
145
|
+
def singular_name
|
146
|
+
model_name.demodulize.singularize
|
147
|
+
end
|
148
|
+
|
149
|
+
# Get plural name
|
150
|
+
def plural_name
|
151
|
+
model_name.demodulize.pluralize
|
152
|
+
end
|
153
|
+
|
154
|
+
# Set single record
|
155
|
+
def record=(value)
|
156
|
+
instance_variable_set "@#{singular_name}", value
|
157
|
+
end
|
158
|
+
|
159
|
+
# Set records collection
|
160
|
+
def records=(value)
|
161
|
+
instance_variable_set "@#{plural_name}", value
|
162
|
+
end
|
163
|
+
|
164
|
+
# Use callbacks to share common setup or constraints between actions.
|
165
|
+
def set_record
|
166
|
+
self.record = model.find(params[:id])
|
167
|
+
end
|
168
|
+
|
169
|
+
# Only allow a trusted parameter "white list" through.
|
170
|
+
def record_params
|
171
|
+
params.require(:"#{singular_name}").permit self.permitted_params
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ActionCrud
|
2
|
+
module Helpers
|
3
|
+
class Link
|
4
|
+
attr_accessor :record, :action, :label, :options
|
5
|
+
|
6
|
+
# Initialize link generator
|
7
|
+
def initialize(context, record=nil, action=nil, *args)
|
8
|
+
@options = args.extract_options!
|
9
|
+
@context = context
|
10
|
+
@record = record || @context.try(:current_record)
|
11
|
+
@action = action
|
12
|
+
@label = options.fetch :label, nil
|
13
|
+
@options = options.except :label
|
14
|
+
|
15
|
+
if delete?
|
16
|
+
@options.reverse_merge!(method: :delete, data: { confirm: 'Are you sure?' })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Is delete action
|
21
|
+
def delete?
|
22
|
+
action.in? [:delete, :destroy]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get link label
|
26
|
+
def action_label
|
27
|
+
label || "#{action}".humanize
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get record path
|
31
|
+
def record_path
|
32
|
+
ActionCrud::Helpers::Route.new(@context, record, action).path || "#{action}"
|
33
|
+
end
|
34
|
+
|
35
|
+
# Render multiple
|
36
|
+
def render_multiple(*args)
|
37
|
+
default = options
|
38
|
+
options = args.extract_options!
|
39
|
+
actions = args.concat(action).concat(options.keys)
|
40
|
+
links = actions.uniq.map do |item|
|
41
|
+
args = Hash(options[item]).reverse_merge(default)
|
42
|
+
ActionCrud::Helpers::Link.new(@context, record, item, args).render
|
43
|
+
end
|
44
|
+
|
45
|
+
links.join.html_safe
|
46
|
+
end
|
47
|
+
|
48
|
+
# Render link
|
49
|
+
def render
|
50
|
+
@context.link_to action_label, record_path, options if record_path.present?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module ActionCrud
|
2
|
+
module Helpers
|
3
|
+
class Route
|
4
|
+
attr_accessor :record, :action, :options, :path, :url
|
5
|
+
|
6
|
+
# Intialize url finder
|
7
|
+
def initialize(context, record=nil, action=nil, *options)
|
8
|
+
@context = context
|
9
|
+
@record = record || @context.try(:current_record)
|
10
|
+
@action = action
|
11
|
+
@options = options
|
12
|
+
@path = route_uri :path
|
13
|
+
@url = route_uri :url
|
14
|
+
end
|
15
|
+
|
16
|
+
# To string
|
17
|
+
def to_s(type=:path)
|
18
|
+
instance_variable_get("@#{type}").to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
# Is index action
|
22
|
+
def index?
|
23
|
+
action == :index
|
24
|
+
end
|
25
|
+
|
26
|
+
# Should prefix method
|
27
|
+
def prefix?
|
28
|
+
action.in? [:new, :edit]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Should include record
|
32
|
+
def record?
|
33
|
+
action.in? [:show, :edit, :delete, :destroy]
|
34
|
+
end
|
35
|
+
|
36
|
+
# Find route key
|
37
|
+
def route_key
|
38
|
+
singular = 'singular_' unless index?
|
39
|
+
record.model_name.try :"#{singular}route_key"
|
40
|
+
end
|
41
|
+
|
42
|
+
# Find route method
|
43
|
+
def route_uri(type)
|
44
|
+
args = [*options]
|
45
|
+
args = [record, *options] if record?
|
46
|
+
method = "#{route_key}_#{type}"
|
47
|
+
method = "#{action}_#{method}" if prefix?
|
48
|
+
|
49
|
+
@context.try :"#{method}", *args
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module ActionCrud
|
2
|
+
module Helpers
|
3
|
+
module Url
|
4
|
+
# Get record path
|
5
|
+
def record_path(*args)
|
6
|
+
options = args.extract_options!
|
7
|
+
record = args.first
|
8
|
+
|
9
|
+
ActionCrud::Helpers::Route.new(self, record, :show, options).path
|
10
|
+
end
|
11
|
+
|
12
|
+
# Get record absolute url
|
13
|
+
def record_url(*args)
|
14
|
+
options = args.extract_options!
|
15
|
+
record = args.first
|
16
|
+
|
17
|
+
ActionCrud::Helpers::Route.new(self, record, :show, options).url
|
18
|
+
end
|
19
|
+
|
20
|
+
# Get records index path
|
21
|
+
def records_path(*args)
|
22
|
+
options = args.extract_options!
|
23
|
+
record = args.first
|
24
|
+
|
25
|
+
ActionCrud::Helpers::Route.new(self, record, :index, options).path
|
26
|
+
end
|
27
|
+
|
28
|
+
# Get records index absolute url
|
29
|
+
def records_url(*args)
|
30
|
+
options = args.extract_options!
|
31
|
+
record = args.first
|
32
|
+
|
33
|
+
ActionCrud::Helpers::Route.new(self, record, :index, options).url
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get record new path
|
37
|
+
def new_record_path(*args)
|
38
|
+
options = args.extract_options!
|
39
|
+
record = args.first
|
40
|
+
|
41
|
+
ActionCrud::Helpers::Route.new(self, record, :new, options).path
|
42
|
+
end
|
43
|
+
|
44
|
+
# Get record new absolute url
|
45
|
+
def new_record_url(*args)
|
46
|
+
options = args.extract_options!
|
47
|
+
record = args.first
|
48
|
+
|
49
|
+
ActionCrud::Helpers::Route.new(self, record, :new, options).url
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get record edit path
|
53
|
+
def edit_record_path(*args)
|
54
|
+
options = args.extract_options!
|
55
|
+
record = args.first
|
56
|
+
|
57
|
+
ActionCrud::Helpers::Route.new(self, record, :edit, options).path
|
58
|
+
end
|
59
|
+
|
60
|
+
# Get record edit absolute url
|
61
|
+
def edit_record_url(*args)
|
62
|
+
options = args.extract_options!
|
63
|
+
record = args.first
|
64
|
+
|
65
|
+
ActionCrud::Helpers::Route.new(self, record, :edit, options).url
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ActionCrud
|
2
|
+
module Helpers
|
3
|
+
module View
|
4
|
+
# Get current model
|
5
|
+
def current_model
|
6
|
+
controller.try(:current_model)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Get current record
|
10
|
+
def current_record
|
11
|
+
controller.try(:current_record) || current_model.try(:new)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Get current records
|
15
|
+
def current_records
|
16
|
+
controller.try(:current_records) || []
|
17
|
+
end
|
18
|
+
|
19
|
+
# Get permitted parameters
|
20
|
+
def permitted_params
|
21
|
+
controller.try(:permitted_params) || []
|
22
|
+
end
|
23
|
+
|
24
|
+
# Get record link
|
25
|
+
def record_link_to(record=nil, *args)
|
26
|
+
options = args.extract_options!
|
27
|
+
action = args.first
|
28
|
+
|
29
|
+
ActionCrud::Helpers::Link.new(self, record, action, options).render
|
30
|
+
end
|
31
|
+
|
32
|
+
# Get record links
|
33
|
+
def record_links_to(record=nil, *args)
|
34
|
+
options = args.extract_options!
|
35
|
+
default = options.fetch :html, {}
|
36
|
+
options = options.except(:html)
|
37
|
+
actions = args.concat(options.keys)
|
38
|
+
actions = actions.any? ? actions : [:show, :edit, :destroy]
|
39
|
+
|
40
|
+
ActionCrud::Helpers::Link.new(self, record, actions, default).render_multiple(options)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module ActionCrud
|
2
|
+
module Pagination
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
# Class attributes
|
7
|
+
class_attribute :per_page, instance_predicate: false
|
8
|
+
|
9
|
+
# Class attributes defaults
|
10
|
+
self.per_page = 20
|
11
|
+
|
12
|
+
# Action callbacks
|
13
|
+
before_action :set_pagination_params, only: [:index]
|
14
|
+
end
|
15
|
+
|
16
|
+
class_methods do
|
17
|
+
# Set per page limit
|
18
|
+
def set_per_page(limit)
|
19
|
+
self.per_page = limit.to_i
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Get pagination params
|
26
|
+
def pagination_params
|
27
|
+
@pagination_params ||= params.permit(:page, :per_page).to_h.deep_symbolize_keys
|
28
|
+
end
|
29
|
+
|
30
|
+
# Set pagination parameters
|
31
|
+
def set_pagination_params
|
32
|
+
pagination = pagination_params[:page].is_a?(Hash) ? pagination_params[:page] : {}
|
33
|
+
pagination_params[:page] = pagination[:number] || pagination_params.fetch(:page, 1)
|
34
|
+
pagination_params[:per_page] = pagination[:size] || pagination_params.fetch(:per_page, per_page)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Paginate records
|
38
|
+
def paginate(records)
|
39
|
+
if records.respond_to? :paginate
|
40
|
+
records.paginate(pagination_params.select { |k, _v| k.in? [:page, :per_page] })
|
41
|
+
else
|
42
|
+
records
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Pagination metadata
|
47
|
+
def pagination_meta(records)
|
48
|
+
{
|
49
|
+
current_page: pagination_params.fetch(:page, 1).to_i,
|
50
|
+
next_page: records.try(:next_page),
|
51
|
+
previous_page: records.try(:previous_page),
|
52
|
+
total_pages: records.try(:total_pages),
|
53
|
+
total_entries: records.try(:total_entries)
|
54
|
+
}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_crud
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonian Guveli
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.8'
|
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'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
description: Description.
|
84
|
+
email:
|
85
|
+
- jonian@hardpixel.eu
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/action_crud.rb
|
96
|
+
- lib/action_crud/controller.rb
|
97
|
+
- lib/action_crud/helpers/link.rb
|
98
|
+
- lib/action_crud/helpers/route.rb
|
99
|
+
- lib/action_crud/helpers/url.rb
|
100
|
+
- lib/action_crud/helpers/view.rb
|
101
|
+
- lib/action_crud/pagination.rb
|
102
|
+
- lib/action_crud/version.rb
|
103
|
+
homepage: https://github.com/hardpixel/active-delegate
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.6.13
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Summary
|
127
|
+
test_files: []
|