action_crud 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9a850a21be41f3a823a55015ef8897dd07cb630
4
- data.tar.gz: a90605864683854c1de1de90885b0375723be8c6
3
+ metadata.gz: d3ed1ca5401fa0b8ec39ec1bb97d36698754f201
4
+ data.tar.gz: cc888e1468207682c216dc767f62fe6a24143503
5
5
  SHA512:
6
- metadata.gz: f757044a3224b97f86b1d70d9c8abcbf6766d624eccb24bbf325015a3a9a7227927f3d64ac8818c6b5e89af27c1a76cce6c5ab4d5dd9b9c4a2a9081f88469acc
7
- data.tar.gz: 29dd59f5a4e0f5ef5290c97d0d910d2ae4d1b3cef51f85621306c6b13699fd59dfeb045ceb8ce1d8a0a027c4ea364ea6c9c819b23c9aa18a40e43b8111f2da00
6
+ metadata.gz: '038320aa145c60b315278c9a42f8888e1cdde284a3ebf0066ccfb21a3ef039a644ca35920604129ea250660a6ed13369485f74b8d9495af7a9c961125cc76115'
7
+ data.tar.gz: 6daa58a28127542dec00fd6718f3972c22aa1dfbd7814a5cffcf575bd96a9e433bf4ee3b1bec1c82d275f09632d597e9508e76a8d0f230bac57960114a7c8bab
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- action_crud (0.1.0)
4
+ action_crud (0.1.1)
5
5
  actionpack (~> 5.0)
6
6
  i18n (~> 0.8)
7
7
 
data/README.md CHANGED
@@ -4,7 +4,7 @@ ActionCrud speeds up development by making your controllers inherit all restful
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/action_crud.svg)](https://badge.fury.io/rb/action_crud)
6
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)
7
+ [![Code Climate](https://codeclimate.com/github/hardpixel/action-crud/badges/gpa.png)](https://codeclimate.com/github/hardpixel/action-crud)
8
8
 
9
9
  ## Installation
10
10
 
@@ -32,7 +32,7 @@ class Post < ActionController::Base
32
32
  end
33
33
  ```
34
34
 
35
- To set the controller model, use the `set_model_name` function:
35
+ To set the controller model and class, use the `set_model_name` and `set_model_class` functions:
36
36
 
37
37
  ```ruby
38
38
  class Post < ActionController::Base
@@ -41,6 +41,12 @@ class Post < ActionController::Base
41
41
 
42
42
  # Or by setting the model_name class attribute
43
43
  self.model_name = 'Post'
44
+
45
+ # Using the set_model_class function
46
+ set_model_class 'Post'
47
+
48
+ # Or by setting the model_class class attribute
49
+ self.model_class = 'Post'
44
50
  end
45
51
  ```
46
52
 
@@ -51,7 +57,7 @@ class Post < ActionController::Base
51
57
  # Using the set_index_scope function
52
58
  set_index_scope :published
53
59
 
54
- # Or by setting the model_name class attribute
60
+ # Or by setting the index_scope class attribute
55
61
  self.index_scope = :published
56
62
  end
57
63
  ```
@@ -75,7 +81,19 @@ class Post < ActionController::Base
75
81
  end
76
82
  ```
77
83
 
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!
84
+ If you use a pagination gem like [SmartPagination](https://github.com/hardpixel/smart-pagination), the index records will be automagically paginated. To set the results limit per page (default: 20), use the `set_per_page` function:
85
+
86
+ ```ruby
87
+ class Post < ActionController::Base
88
+ # Using the set_per_page function
89
+ set_per_page 10
90
+
91
+ # Or by setting the per_page class attribute
92
+ self.per_page = 10
93
+ end
94
+ ```
95
+
96
+ After setting up the controller, like the examples above, you will have a fully working CRUD controller with instance variables `@post` and `@posts` available.
79
97
 
80
98
  ActionCrud also injects in your views and controllers the following helpers:
81
99
 
data/lib/action_crud.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'active_support'
1
2
  require 'action_crud/controller'
2
3
  require 'action_crud/pagination'
3
4
  require 'action_crud/helpers/route'
@@ -5,14 +5,20 @@ module ActionCrud
5
5
  included do
6
6
  # Class attributes
7
7
  class_attribute :model_name, instance_predicate: false
8
+ class_attribute :model_class, instance_predicate: false
9
+ class_attribute :instance_name, instance_predicate: false
10
+ class_attribute :collection_name, instance_predicate: false
8
11
  class_attribute :index_scope, instance_predicate: false
9
12
  class_attribute :permitted_params, instance_predicate: false
10
13
 
11
14
  # Class attributes defaults
12
- self.model_name = self.controller_name
13
- self.index_scope = :all
14
15
  self.permitted_params = []
15
16
 
17
+ # Set class attributes
18
+ set_model_name
19
+ set_model_class
20
+ set_index_scope
21
+
16
22
  # Action callbacks
17
23
  before_action :set_record, only: [:show, :edit, :update, :destroy]
18
24
  end
@@ -20,8 +26,7 @@ module ActionCrud
20
26
  class_methods do
21
27
  # Set permitted parameters
22
28
  def permit_params(options={})
23
- model = self.model_name.classify.constantize
24
- default = { only: model.attribute_names, except: [], also: [], array: [], hash: [] }
29
+ default = { only: model_class.attribute_names, except: [], also: [], array: [], hash: [] }
25
30
  options = Hash[default.merge(options).map { |k, v| [k, Array(v).map(&:to_sym)] }]
26
31
  permit = options.except(:except).values.flatten.uniq
27
32
 
@@ -33,19 +38,30 @@ module ActionCrud
33
38
  end
34
39
 
35
40
  # Set model name
36
- def set_model_name(name)
37
- self.model_name = name
41
+ def set_model_name(name=nil)
42
+ name = name || controller_name.classify
43
+ name = name.constantize if name.is_a? String
44
+
45
+ self.model_name = name.model_name
46
+ self.instance_name = model_name.singular
47
+ self.collection_name = model_name.plural
48
+ end
49
+
50
+ # Set model class
51
+ def set_model_class(klass=nil)
52
+ klass = klass.constantize if klass.is_a? String
53
+ self.model_class = klass || model_name.instance_variable_get('@klass')
38
54
  end
39
55
 
40
56
  # Set index scope
41
- def set_index_scope(scope)
42
- self.index_scope = scope
57
+ def set_index_scope(scope='all')
58
+ self.index_scope = scope.to_sym
43
59
  end
44
60
  end
45
61
 
46
62
  # GET /model
47
63
  def index
48
- self.records = model.send self.index_scope
64
+ self.records = model.send index_scope
49
65
  self.records = paginate(records) if respond_to? :per_page
50
66
 
51
67
  respond_to do |format|
@@ -120,45 +136,35 @@ module ActionCrud
120
136
 
121
137
  # Get model
122
138
  def model
123
- model_name.classify.constantize
139
+ model_class
124
140
  end
125
141
 
126
142
  alias :current_model :model
127
143
 
128
144
  # Get single record
129
145
  def record
130
- instance_variable_get "@#{singular_name}"
146
+ instance_variable_get "@#{instance_name}"
131
147
  end
132
148
 
133
149
  alias :current_record :record
134
150
 
135
151
  # Get records collection
136
152
  def records
137
- instance_variable_get "@#{plural_name}"
153
+ instance_variable_get "@#{collection_name}"
138
154
  end
139
155
 
140
156
  alias :current_records :records
141
157
 
142
158
  private
143
159
 
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
160
  # Set single record
155
161
  def record=(value)
156
- instance_variable_set "@#{singular_name}", value
162
+ instance_variable_set "@#{instance_name}", value
157
163
  end
158
164
 
159
165
  # Set records collection
160
166
  def records=(value)
161
- instance_variable_set "@#{plural_name}", value
167
+ instance_variable_set "@#{collection_name}", value
162
168
  end
163
169
 
164
170
  # Use callbacks to share common setup or constraints between actions.
@@ -168,7 +174,7 @@ module ActionCrud
168
174
 
169
175
  # Only allow a trusted parameter "white list" through.
170
176
  def record_params
171
- params.require(:"#{singular_name}").permit self.permitted_params
177
+ params.require(:"#{instance_name}").permit permitted_params
172
178
  end
173
179
  end
174
180
  end
@@ -1,3 +1,3 @@
1
1
  module ActionCrud
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_crud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonian Guveli
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-04 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack