dry_crud 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/VERSION +1 -1
- data/lib/generators/dry_crud/dry_crud_generator.rb +4 -0
- data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +5 -1
- data/lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb +2 -1
- data/lib/generators/dry_crud/templates/test/helpers/i18n_helper_test.rb +1 -0
- data/lib/generators/dry_crud/templates/test/support/crud_test_helper.rb +188 -0
- data/lib/generators/dry_crud/templates/test/support/crud_test_model.rb +0 -301
- data/lib/generators/dry_crud/templates/test/support/crud_test_models_controller.rb +117 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzE5OGExYmQyZDhkN2JiNGVmZjNjZGU5NTBlMTliZWU5ZTA5NDVkYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Yzk1Y2M5ZjJlZDk0MWYwZWUzMjA2NTc4YWMwZmUzMTlmYTdlODFjMw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OThmZjNlNTJhZDRkNWI5M2ZhYTBmYzQ1OGFlYTA0ZTVkYjFlZDkxN2U0MWNk
|
10
|
+
OGQzMzViYWU3OWQwY2EzMjE4ZTc4OWNlYWMwNmVjNmRlNjY3N2VjMjY5YzNk
|
11
|
+
ODhiZDAzZjUzYjVkZTMyM2ZmMDljNDNlYzRmZTM3ZDg4MTZiOWE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MThmZTk3YTQ5YWY0YWZlNDI5MDFlMmE3ZjAxZDgxNmU2ZDljN2UyNWFlZWI3
|
14
|
+
OTY2NmQxYWUyMzQyYmZhNGZlMDU3NmYyNmVhYTRmMDc5MzhkNDlmOTkzNzFl
|
15
|
+
ZGQ1OGYxNGIyNDgyY2U1ZGE5YzA5OGY0MWQ4ODMzNGJlOGJiNjM=
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1.
|
1
|
+
2.1.1
|
@@ -37,6 +37,10 @@ class DryCrudGenerator < DryCrudGeneratorBase
|
|
37
37
|
unless exclude_test_dir == 'spec'
|
38
38
|
template(File.join('test', 'support', 'crud_test_model.rb'),
|
39
39
|
File.join('spec', 'support', 'crud_test_model.rb'))
|
40
|
+
template(File.join('test', 'support', 'crud_test_models_controller.rb'),
|
41
|
+
File.join('spec', 'support', 'crud_test_models_controller.rb'))
|
42
|
+
template(File.join('test', 'support', 'crud_test_helper.rb'),
|
43
|
+
File.join('spec', 'support', 'crud_test_helper.rb'))
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
@@ -12,7 +12,11 @@ module CrudControllerTestHelper
|
|
12
12
|
params = scope_params.merge(format: m[:format])
|
13
13
|
params.merge!(id: test_entry.id) if m[:id]
|
14
14
|
params.merge!(example_params)
|
15
|
-
|
15
|
+
if m[:method] == :get && m[:format] == :js
|
16
|
+
xhr(:get, m[:action], params)
|
17
|
+
else
|
18
|
+
send(m[:method], m[:action], params)
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
# If a combine key is given in metadata, only the first request for all
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require 'test_helper'
|
3
|
+
require 'support/crud_test_helper'
|
3
4
|
require 'support/crud_test_model'
|
4
5
|
require 'support/crud_controller_test_helper'
|
5
6
|
|
@@ -39,7 +40,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def test_index_js
|
42
|
-
get :index, test_params
|
43
|
+
xhr :get, :index, test_params
|
43
44
|
assert_response :success
|
44
45
|
assert_equal 'index js', @response.body
|
45
46
|
assert entries.present?
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#:nodoc:
|
4
|
+
REGEXP_ROWS = /<tr.+?<\/tr>/m #:nodoc:
|
5
|
+
REGEXP_HEADERS = /<th.+?<\/th>/m #:nodoc:
|
6
|
+
REGEXP_SORT_HEADERS = /<th.*?><a .*?sort_dir=asc.*?>.*?<\/a><\/th>/m #:nodoc:
|
7
|
+
REGEXP_ACTION_CELL = /<td class=\"action\"><a .*?href.+?<\/a><\/td>/m #:nodoc:
|
8
|
+
|
9
|
+
# A simple test helper to prepare the test database with a CrudTestModel model.
|
10
|
+
# This helper is used to test the CrudController and various helpers
|
11
|
+
# without the need for an application based model.
|
12
|
+
module CrudTestHelper
|
13
|
+
|
14
|
+
# Controller helper methods for the tests
|
15
|
+
|
16
|
+
def model_class
|
17
|
+
CrudTestModel
|
18
|
+
end
|
19
|
+
|
20
|
+
def controller_name
|
21
|
+
'crud_test_models'
|
22
|
+
end
|
23
|
+
|
24
|
+
def action_name
|
25
|
+
'index'
|
26
|
+
end
|
27
|
+
|
28
|
+
def params
|
29
|
+
{}
|
30
|
+
end
|
31
|
+
|
32
|
+
def path_args(entry)
|
33
|
+
entry
|
34
|
+
end
|
35
|
+
|
36
|
+
def sortable?(attr)
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
def h(text)
|
41
|
+
ERB::Util.h(text)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
# Sets up the test database with a crud_test_models table.
|
47
|
+
# Look at the source to view the column definition.
|
48
|
+
def setup_db
|
49
|
+
without_transaction do
|
50
|
+
silence_stream(STDOUT) do
|
51
|
+
c = ActiveRecord::Base.connection
|
52
|
+
|
53
|
+
create_crud_test_models(c)
|
54
|
+
create_other_crud_test_models(c)
|
55
|
+
create_crud_test_models_other_crud_test_models(c)
|
56
|
+
end
|
57
|
+
|
58
|
+
CrudTestModel.reset_column_information
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_crud_test_models(c)
|
63
|
+
c.create_table :crud_test_models, force: true do |t|
|
64
|
+
t.string :name, null: false, limit: 50
|
65
|
+
t.string :email
|
66
|
+
t.string :password
|
67
|
+
t.string :whatever
|
68
|
+
t.integer :children
|
69
|
+
t.integer :companion_id
|
70
|
+
t.float :rating
|
71
|
+
t.decimal :income, precision: 14, scale: 4
|
72
|
+
t.date :birthdate
|
73
|
+
t.time :gets_up_at
|
74
|
+
t.datetime :last_seen
|
75
|
+
t.boolean :human, default: true
|
76
|
+
t.text :remarks
|
77
|
+
|
78
|
+
t.timestamps
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def create_other_crud_test_models(c)
|
83
|
+
c.create_table :other_crud_test_models, force: true do |t|
|
84
|
+
t.string :name, null: false, limit: 50
|
85
|
+
t.integer :more_id
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def create_crud_test_models_other_crud_test_models(c)
|
90
|
+
c.create_table :crud_test_models_other_crud_test_models,
|
91
|
+
force: true do |t|
|
92
|
+
t.belongs_to :crud_test_model
|
93
|
+
t.belongs_to :other_crud_test_model
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Removes the crud_test_models table from the database.
|
98
|
+
def reset_db
|
99
|
+
c = ActiveRecord::Base.connection
|
100
|
+
[:crud_test_models,
|
101
|
+
:other_crud_test_models,
|
102
|
+
:crud_test_models_other_crud_test_models].each do |table|
|
103
|
+
c.drop_table(table) if c.table_exists?(table)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Creates 6 dummy entries for the crud_test_models table.
|
108
|
+
def create_test_data
|
109
|
+
(1..6).reduce(nil) { |a, e| create(e, a) }
|
110
|
+
(1..6).each { |i| create_other(i) }
|
111
|
+
end
|
112
|
+
|
113
|
+
# Fixture-style accessor method to get CrudTestModel instances by name
|
114
|
+
def crud_test_models(name)
|
115
|
+
CrudTestModel.find_by_name(name.to_s)
|
116
|
+
end
|
117
|
+
|
118
|
+
def with_test_routing
|
119
|
+
with_routing do |set|
|
120
|
+
set.draw { resources :crud_test_models }
|
121
|
+
# used to define a controller in these tests
|
122
|
+
set.default_url_options = { controller: 'crud_test_models' }
|
123
|
+
yield
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def special_routing
|
128
|
+
# test:unit uses instance variable, rspec the method
|
129
|
+
controller = @controller || @_controller || controller
|
130
|
+
@routes = ActionDispatch::Routing::RouteSet.new
|
131
|
+
_routes = @routes
|
132
|
+
|
133
|
+
controller.singleton_class.send(:include, _routes.url_helpers)
|
134
|
+
controller.view_context_class = Class.new(controller.view_context_class) do
|
135
|
+
include _routes.url_helpers
|
136
|
+
end
|
137
|
+
|
138
|
+
@routes.draw { resources :crud_test_models }
|
139
|
+
end
|
140
|
+
|
141
|
+
def create(index, companion)
|
142
|
+
c = str(index)
|
143
|
+
m = CrudTestModel.new(
|
144
|
+
name: c,
|
145
|
+
children: 10 - index,
|
146
|
+
rating: "#{index}.#{index}".to_f,
|
147
|
+
income: 10_000_000 * index + 0.1111 * index,
|
148
|
+
birthdate: "#{1900 + 10 * index}-#{index}-#{index}",
|
149
|
+
# store entire date to avoid time zone issues
|
150
|
+
gets_up_at: Time.zone.local(2000, 1, 1, index, index),
|
151
|
+
last_seen: "#{2000 + 10 * index}-#{index}-#{index} " +
|
152
|
+
"1#{index}:2#{index}",
|
153
|
+
human: index.even?,
|
154
|
+
remarks: "#{c} #{str(index + 1)} #{str(index + 2)}\n" *
|
155
|
+
(index % 3 + 1))
|
156
|
+
m.companion = companion
|
157
|
+
m.save!
|
158
|
+
m
|
159
|
+
end
|
160
|
+
|
161
|
+
def create_other(index)
|
162
|
+
c = str(index)
|
163
|
+
others = CrudTestModel.all[index..(index + 2)]
|
164
|
+
OtherCrudTestModel.create!(name: c,
|
165
|
+
other_ids: others.map(&:id),
|
166
|
+
more_id: others.first.try(:id))
|
167
|
+
end
|
168
|
+
|
169
|
+
def str(index)
|
170
|
+
(index + 64).chr * 5
|
171
|
+
end
|
172
|
+
|
173
|
+
# A hack to avoid ddl in transaction issues with mysql.
|
174
|
+
def without_transaction
|
175
|
+
c = ActiveRecord::Base.connection
|
176
|
+
start_transaction = false
|
177
|
+
if c.adapter_name.downcase.include?('mysql') && c.open_transactions > 0
|
178
|
+
# in transactional tests, we may simply rollback
|
179
|
+
c.execute('ROLLBACK')
|
180
|
+
start_transaction = true
|
181
|
+
end
|
182
|
+
|
183
|
+
yield
|
184
|
+
|
185
|
+
c.execute('BEGIN') if start_transaction
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
@@ -47,305 +47,4 @@ class OtherCrudTestModel < ActiveRecord::Base #:nodoc:
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
# Controller for the dummy model.
|
51
|
-
class CrudTestModelsController < CrudController #:nodoc:
|
52
|
-
HANDLE_PREFIX = 'handle_'
|
53
50
|
|
54
|
-
self.search_columns = [:name, :whatever, :remarks]
|
55
|
-
self.sort_mappings = { chatty: 'length(remarks)' }
|
56
|
-
self.default_sort = 'name'
|
57
|
-
<% if Rails.version >= '4.0' -%>
|
58
|
-
self.permitted_attrs = [:name, :email, :password, :whatever, :children,
|
59
|
-
:companion_id, :rating, :income, :birthdate,
|
60
|
-
:gets_up_at, :last_seen, :human, :remarks]
|
61
|
-
<% end -%>
|
62
|
-
|
63
|
-
before_create :possibly_redirect
|
64
|
-
before_create :handle_name
|
65
|
-
before_destroy :handle_name
|
66
|
-
|
67
|
-
before_render_new :possibly_redirect
|
68
|
-
before_render_new :set_companions
|
69
|
-
|
70
|
-
attr_reader :called_callbacks
|
71
|
-
attr_accessor :should_redirect
|
72
|
-
|
73
|
-
hide_action :called_callbacks, :should_redirect, :should_redirect=
|
74
|
-
|
75
|
-
# don't use the standard layout as it may require different routes
|
76
|
-
# than just the test route for this controller
|
77
|
-
layout false
|
78
|
-
|
79
|
-
def index
|
80
|
-
super do |format|
|
81
|
-
format.js { render text: 'index js'}
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def show
|
86
|
-
super do |format|
|
87
|
-
format.html { render text: 'custom html' } if entry.name == 'BBBBB'
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def create
|
92
|
-
super do |format|
|
93
|
-
flash[:notice] = 'model got created' if entry.persisted?
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
private
|
98
|
-
|
99
|
-
def list_entries
|
100
|
-
entries = super
|
101
|
-
if params[:filter]
|
102
|
-
entries = entries.where(['rating < ?', 3])
|
103
|
-
.except(:order)
|
104
|
-
.order('children DESC')
|
105
|
-
end
|
106
|
-
entries
|
107
|
-
end
|
108
|
-
|
109
|
-
private
|
110
|
-
|
111
|
-
def build_entry
|
112
|
-
entry = super
|
113
|
-
if params[model_identifier]
|
114
|
-
entry.companion_id = model_params.delete(:companion_id)
|
115
|
-
end
|
116
|
-
entry
|
117
|
-
end
|
118
|
-
|
119
|
-
# custom callback
|
120
|
-
def handle_name
|
121
|
-
if entry.name == 'illegal'
|
122
|
-
flash[:alert] = 'illegal name'
|
123
|
-
false
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
# callback to redirect if @should_redirect is set
|
128
|
-
def possibly_redirect
|
129
|
-
redirect_to action: 'index' if should_redirect && !performed?
|
130
|
-
!should_redirect
|
131
|
-
end
|
132
|
-
|
133
|
-
def set_companions
|
134
|
-
@companions = CrudTestModel.where(human: true)
|
135
|
-
end
|
136
|
-
|
137
|
-
# create callback methods that record the before/after callbacks
|
138
|
-
[:create, :update, :save, :destroy].each do |a|
|
139
|
-
callback = "before_#{a.to_s}"
|
140
|
-
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
141
|
-
callback = "after_#{a.to_s}"
|
142
|
-
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
143
|
-
end
|
144
|
-
|
145
|
-
# create callback methods that record the before_render callbacks
|
146
|
-
[:index, :show, :new, :edit, :form].each do |a|
|
147
|
-
callback = "before_render_#{a.to_s}"
|
148
|
-
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
149
|
-
end
|
150
|
-
|
151
|
-
# handle the called callbacks
|
152
|
-
def method_missing(sym, *args)
|
153
|
-
if sym.to_s.starts_with?(HANDLE_PREFIX)
|
154
|
-
called_callback(sym.to_s[HANDLE_PREFIX.size..-1].to_sym)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
# records a callback
|
159
|
-
def called_callback(callback)
|
160
|
-
@called_callbacks ||= []
|
161
|
-
@called_callbacks << callback
|
162
|
-
end
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
#:nodoc:
|
167
|
-
REGEXP_ROWS = /<tr.+?<\/tr>/m #:nodoc:
|
168
|
-
REGEXP_HEADERS = /<th.+?<\/th>/m #:nodoc:
|
169
|
-
REGEXP_SORT_HEADERS = /<th.*?><a .*?sort_dir=asc.*?>.*?<\/a><\/th>/m #:nodoc:
|
170
|
-
REGEXP_ACTION_CELL = /<td class=\"action\"><a .*?href.+?<\/a><\/td>/m #:nodoc:
|
171
|
-
|
172
|
-
# A simple test helper to prepare the test database with a CrudTestModel model.
|
173
|
-
# This helper is used to test the CrudController and various helpers
|
174
|
-
# without the need for an application based model.
|
175
|
-
module CrudTestHelper
|
176
|
-
|
177
|
-
# Controller helper methods for the tests
|
178
|
-
|
179
|
-
def model_class
|
180
|
-
CrudTestModel
|
181
|
-
end
|
182
|
-
|
183
|
-
def controller_name
|
184
|
-
'crud_test_models'
|
185
|
-
end
|
186
|
-
|
187
|
-
def action_name
|
188
|
-
'index'
|
189
|
-
end
|
190
|
-
|
191
|
-
def params
|
192
|
-
{}
|
193
|
-
end
|
194
|
-
|
195
|
-
def path_args(entry)
|
196
|
-
entry
|
197
|
-
end
|
198
|
-
|
199
|
-
def sortable?(attr)
|
200
|
-
true
|
201
|
-
end
|
202
|
-
|
203
|
-
def h(text)
|
204
|
-
ERB::Util.h(text)
|
205
|
-
end
|
206
|
-
|
207
|
-
private
|
208
|
-
|
209
|
-
# Sets up the test database with a crud_test_models table.
|
210
|
-
# Look at the source to view the column definition.
|
211
|
-
def setup_db
|
212
|
-
without_transaction do
|
213
|
-
silence_stream(STDOUT) do
|
214
|
-
c = ActiveRecord::Base.connection
|
215
|
-
|
216
|
-
create_crud_test_models(c)
|
217
|
-
create_other_crud_test_models(c)
|
218
|
-
create_crud_test_models_other_crud_test_models(c)
|
219
|
-
end
|
220
|
-
|
221
|
-
CrudTestModel.reset_column_information
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
def create_crud_test_models(c)
|
226
|
-
c.create_table :crud_test_models, force: true do |t|
|
227
|
-
t.string :name, null: false, limit: 50
|
228
|
-
t.string :email
|
229
|
-
t.string :password
|
230
|
-
t.string :whatever
|
231
|
-
t.integer :children
|
232
|
-
t.integer :companion_id
|
233
|
-
t.float :rating
|
234
|
-
t.decimal :income, precision: 14, scale: 4
|
235
|
-
t.date :birthdate
|
236
|
-
t.time :gets_up_at
|
237
|
-
t.datetime :last_seen
|
238
|
-
t.boolean :human, default: true
|
239
|
-
t.text :remarks
|
240
|
-
|
241
|
-
t.timestamps
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
def create_other_crud_test_models(c)
|
246
|
-
c.create_table :other_crud_test_models, force: true do |t|
|
247
|
-
t.string :name, null: false, limit: 50
|
248
|
-
t.integer :more_id
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
def create_crud_test_models_other_crud_test_models(c)
|
253
|
-
c.create_table :crud_test_models_other_crud_test_models,
|
254
|
-
force: true do |t|
|
255
|
-
t.belongs_to :crud_test_model
|
256
|
-
t.belongs_to :other_crud_test_model
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
# Removes the crud_test_models table from the database.
|
261
|
-
def reset_db
|
262
|
-
c = ActiveRecord::Base.connection
|
263
|
-
[:crud_test_models,
|
264
|
-
:other_crud_test_models,
|
265
|
-
:crud_test_models_other_crud_test_models].each do |table|
|
266
|
-
c.drop_table(table) if c.table_exists?(table)
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
|
-
# Creates 6 dummy entries for the crud_test_models table.
|
271
|
-
def create_test_data
|
272
|
-
(1..6).reduce(nil) { |a, e| create(e, a) }
|
273
|
-
(1..6).each { |i| create_other(i) }
|
274
|
-
end
|
275
|
-
|
276
|
-
# Fixture-style accessor method to get CrudTestModel instances by name
|
277
|
-
def crud_test_models(name)
|
278
|
-
CrudTestModel.find_by_name(name.to_s)
|
279
|
-
end
|
280
|
-
|
281
|
-
def with_test_routing
|
282
|
-
with_routing do |set|
|
283
|
-
set.draw { resources :crud_test_models }
|
284
|
-
# used to define a controller in these tests
|
285
|
-
set.default_url_options = { controller: 'crud_test_models' }
|
286
|
-
yield
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
|
-
def special_routing
|
291
|
-
# test:unit uses instance variable, rspec the method
|
292
|
-
controller = @controller || controller
|
293
|
-
@routes = ActionDispatch::Routing::RouteSet.new
|
294
|
-
_routes = @routes
|
295
|
-
|
296
|
-
controller.singleton_class.send(:include, _routes.url_helpers)
|
297
|
-
controller.view_context_class = Class.new(controller.view_context_class) do
|
298
|
-
include _routes.url_helpers
|
299
|
-
end
|
300
|
-
|
301
|
-
@routes.draw { resources :crud_test_models }
|
302
|
-
end
|
303
|
-
|
304
|
-
def create(index, companion)
|
305
|
-
c = str(index)
|
306
|
-
m = CrudTestModel.new(
|
307
|
-
name: c,
|
308
|
-
children: 10 - index,
|
309
|
-
rating: "#{index}.#{index}".to_f,
|
310
|
-
income: 10_000_000 * index + 0.1111 * index,
|
311
|
-
birthdate: "#{1900 + 10 * index}-#{index}-#{index}",
|
312
|
-
# store entire date to avoid time zone issues
|
313
|
-
gets_up_at: Time.zone.local(2000, 1, 1, index, index),
|
314
|
-
last_seen: "#{2000 + 10 * index}-#{index}-#{index} " +
|
315
|
-
"1#{index}:2#{index}",
|
316
|
-
human: index.even?,
|
317
|
-
remarks: "#{c} #{str(index + 1)} #{str(index + 2)}\n" *
|
318
|
-
(index % 3 + 1))
|
319
|
-
m.companion = companion
|
320
|
-
m.save!
|
321
|
-
m
|
322
|
-
end
|
323
|
-
|
324
|
-
def create_other(index)
|
325
|
-
c = str(index)
|
326
|
-
others = CrudTestModel.all[index..(index + 2)]
|
327
|
-
OtherCrudTestModel.create!(name: c,
|
328
|
-
other_ids: others.map(&:id),
|
329
|
-
more_id: others.first.try(:id))
|
330
|
-
end
|
331
|
-
|
332
|
-
def str(index)
|
333
|
-
(index + 64).chr * 5
|
334
|
-
end
|
335
|
-
|
336
|
-
# A hack to avoid ddl in transaction issues with mysql.
|
337
|
-
def without_transaction
|
338
|
-
c = ActiveRecord::Base.connection
|
339
|
-
start_transaction = false
|
340
|
-
if c.adapter_name.downcase.include?('mysql') && c.open_transactions > 0
|
341
|
-
# in transactional tests, we may simply rollback
|
342
|
-
c.execute('ROLLBACK')
|
343
|
-
start_transaction = true
|
344
|
-
end
|
345
|
-
|
346
|
-
yield
|
347
|
-
|
348
|
-
c.execute('BEGIN') if start_transaction
|
349
|
-
end
|
350
|
-
|
351
|
-
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Controller for the dummy model.
|
4
|
+
class CrudTestModelsController < CrudController #:nodoc:
|
5
|
+
HANDLE_PREFIX = 'handle_'
|
6
|
+
|
7
|
+
self.search_columns = [:name, :whatever, :remarks]
|
8
|
+
self.sort_mappings = { chatty: 'length(remarks)' }
|
9
|
+
self.default_sort = 'name'
|
10
|
+
<% if Rails.version >= '4.0' -%>
|
11
|
+
self.permitted_attrs = [:name, :email, :password, :whatever, :children,
|
12
|
+
:companion_id, :rating, :income, :birthdate,
|
13
|
+
:gets_up_at, :last_seen, :human, :remarks]
|
14
|
+
<% end -%>
|
15
|
+
|
16
|
+
before_create :possibly_redirect
|
17
|
+
before_create :handle_name
|
18
|
+
before_destroy :handle_name
|
19
|
+
|
20
|
+
before_render_new :possibly_redirect
|
21
|
+
before_render_new :set_companions
|
22
|
+
|
23
|
+
attr_reader :called_callbacks
|
24
|
+
attr_accessor :should_redirect
|
25
|
+
|
26
|
+
hide_action :called_callbacks, :should_redirect, :should_redirect=
|
27
|
+
|
28
|
+
# don't use the standard layout as it may require different routes
|
29
|
+
# than just the test route for this controller
|
30
|
+
layout false
|
31
|
+
|
32
|
+
def index
|
33
|
+
super do |format|
|
34
|
+
format.js { render text: 'index js'}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def show
|
39
|
+
super do |format|
|
40
|
+
format.html { render text: 'custom html' } if entry.name == 'BBBBB'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def create
|
45
|
+
super do |format|
|
46
|
+
flash[:notice] = 'model got created' if entry.persisted?
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def list_entries
|
53
|
+
entries = super
|
54
|
+
if params[:filter]
|
55
|
+
entries = entries.where(['rating < ?', 3])
|
56
|
+
.except(:order)
|
57
|
+
.order('children DESC')
|
58
|
+
end
|
59
|
+
entries
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def build_entry
|
65
|
+
entry = super
|
66
|
+
if params[model_identifier]
|
67
|
+
entry.companion_id = model_params.delete(:companion_id)
|
68
|
+
end
|
69
|
+
entry
|
70
|
+
end
|
71
|
+
|
72
|
+
# custom callback
|
73
|
+
def handle_name
|
74
|
+
if entry.name == 'illegal'
|
75
|
+
flash[:alert] = 'illegal name'
|
76
|
+
false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# callback to redirect if @should_redirect is set
|
81
|
+
def possibly_redirect
|
82
|
+
redirect_to action: 'index' if should_redirect && !performed?
|
83
|
+
!should_redirect
|
84
|
+
end
|
85
|
+
|
86
|
+
def set_companions
|
87
|
+
@companions = CrudTestModel.where(human: true)
|
88
|
+
end
|
89
|
+
|
90
|
+
# create callback methods that record the before/after callbacks
|
91
|
+
[:create, :update, :save, :destroy].each do |a|
|
92
|
+
callback = "before_#{a.to_s}"
|
93
|
+
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
94
|
+
callback = "after_#{a.to_s}"
|
95
|
+
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
96
|
+
end
|
97
|
+
|
98
|
+
# create callback methods that record the before_render callbacks
|
99
|
+
[:index, :show, :new, :edit, :form].each do |a|
|
100
|
+
callback = "before_render_#{a.to_s}"
|
101
|
+
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
102
|
+
end
|
103
|
+
|
104
|
+
# handle the called callbacks
|
105
|
+
def method_missing(sym, *args)
|
106
|
+
if sym.to_s.starts_with?(HANDLE_PREFIX)
|
107
|
+
called_callback(sym.to_s[HANDLE_PREFIX.size..-1].to_sym)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# records a callback
|
112
|
+
def called_callback(callback)
|
113
|
+
@called_callbacks ||= []
|
114
|
+
@called_callbacks << callback
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry_crud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pascal Zumkehr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -134,7 +134,9 @@ files:
|
|
134
134
|
- lib/generators/dry_crud/templates/test/helpers/table_helper_test.rb
|
135
135
|
- lib/generators/dry_crud/templates/test/helpers/utility_helper_test.rb
|
136
136
|
- lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb
|
137
|
+
- lib/generators/dry_crud/templates/test/support/crud_test_helper.rb
|
137
138
|
- lib/generators/dry_crud/templates/test/support/crud_test_model.rb
|
139
|
+
- lib/generators/dry_crud/templates/test/support/crud_test_models_controller.rb
|
138
140
|
- lib/generators/dry_crud/templates/test/support/custom_assertions.rb
|
139
141
|
- MIT-LICENSE
|
140
142
|
- README.rdoc
|