dry_crud 0.6.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +41 -36
- data/Rakefile +51 -43
- data/VERSION +1 -1
- data/lib/generators/dry_crud/USAGE +1 -0
- data/lib/generators/dry_crud/dry_crud_generator.rb +20 -0
- data/{rails_generators → lib/generators}/dry_crud/templates/INSTALL +4 -3
- data/{rails_generators → lib/generators}/dry_crud/templates/app/controllers/crud_controller.rb +31 -9
- data/lib/generators/dry_crud/templates/app/controllers/render_inheritable.rb +152 -0
- data/{rails_generators → lib/generators}/dry_crud/templates/app/helpers/crud_helper.rb +4 -4
- data/{rails_generators/dry_crud/templates/lib → lib/generators/dry_crud/templates/app/helpers}/standard_form_builder.rb +0 -0
- data/{rails_generators → lib/generators}/dry_crud/templates/app/helpers/standard_helper.rb +81 -76
- data/{rails_generators/dry_crud/templates/lib → lib/generators/dry_crud/templates/app/helpers}/standard_table_builder.rb +45 -46
- data/{rails_generators → lib/generators}/dry_crud/templates/app/views/crud/_attrs.html.erb +0 -0
- data/lib/generators/dry_crud/templates/app/views/crud/_form.html.erb +1 -0
- data/{rails_generators → lib/generators}/dry_crud/templates/app/views/crud/_list.html.erb +0 -0
- data/{rails_generators → lib/generators}/dry_crud/templates/app/views/crud/edit.html.erb +1 -1
- data/{rails_generators → lib/generators}/dry_crud/templates/app/views/crud/index.html.erb +1 -1
- data/{rails_generators → lib/generators}/dry_crud/templates/app/views/crud/new.html.erb +1 -1
- data/{rails_generators → lib/generators}/dry_crud/templates/app/views/crud/show.html.erb +1 -1
- data/{rails_generators → lib/generators}/dry_crud/templates/app/views/layouts/crud.html.erb +1 -0
- data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.erb +10 -0
- data/lib/generators/dry_crud/templates/app/views/shared/_labeled.html.erb +4 -0
- data/{rails_generators → lib/generators}/dry_crud/templates/public/stylesheets/crud.css +5 -5
- data/{rails_generators → lib/generators}/dry_crud/templates/test/crud_test_model.rb +25 -17
- data/{rails_generators → lib/generators}/dry_crud/templates/test/functional/crud_controller_test_helper.rb +36 -32
- data/{rails_generators → lib/generators}/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +17 -4
- data/{rails_generators → lib/generators}/dry_crud/templates/test/unit/crud_helper_test.rb +51 -6
- data/{rails_generators → lib/generators}/dry_crud/templates/test/unit/render_inheritable_test.rb +31 -39
- data/{rails_generators → lib/generators}/dry_crud/templates/test/unit/standard_form_builder_test.rb +0 -0
- data/{rails_generators → lib/generators}/dry_crud/templates/test/unit/standard_helper_test.rb +76 -54
- data/{rails_generators → lib/generators}/dry_crud/templates/test/unit/standard_table_builder_test.rb +42 -40
- data/test/templates/app/views/ajax/ajax.js.rjs +1 -1
- data/test/templates/app/views/ajax/index.html.erb +2 -2
- data/test/templates/app/views/cities/_form.html.erb +1 -1
- data/test/templates/app/views/cities/_list.html.erb +1 -2
- data/test/templates/config/routes.rb +14 -5
- metadata +51 -37
- data/rails_generators/dry_crud/USAGE +0 -1
- data/rails_generators/dry_crud/dry_crud_generator.rb +0 -22
- data/rails_generators/dry_crud/templates/app/views/crud/_form.html.erb +0 -1
- data/rails_generators/dry_crud/templates/app/views/shared/_labeled.html.erb +0 -5
- data/rails_generators/dry_crud/templates/lib/crud_callbacks.rb +0 -55
- data/rails_generators/dry_crud/templates/lib/render_inheritable.rb +0 -118
@@ -45,66 +45,6 @@ module StandardHelper
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
# Formats an active record association
|
49
|
-
def format_assoc(obj, assoc)
|
50
|
-
if assoc_val = obj.send(assoc.name)
|
51
|
-
link_to_unless(no_assoc_link?(assoc), h(assoc_val.label), assoc_val)
|
52
|
-
else
|
53
|
-
'(none)'
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# Returns true if no link should be created when formatting the given association.
|
58
|
-
def no_assoc_link?(assoc)
|
59
|
-
(respond_to?(:no_assoc_links) && no_assoc_links.to_a.include?(assoc.name.to_sym)) ||
|
60
|
-
!respond_to?("#{assoc.klass.name.underscore}_path".to_sym)
|
61
|
-
end
|
62
|
-
|
63
|
-
# Formats an arbitrary attribute of the given object depending on its data type.
|
64
|
-
# For ActiveRecords, take the defined data type into account for special types
|
65
|
-
# that have no own object class.
|
66
|
-
def format_type(obj, attr)
|
67
|
-
val = obj.send(attr)
|
68
|
-
return EMPTY_STRING if val.nil?
|
69
|
-
case column_type(obj, attr)
|
70
|
-
when :time then val.strftime(TIME_FORMAT)
|
71
|
-
when :date then val.to_date.to_s
|
72
|
-
when :text then simple_format(h(val))
|
73
|
-
when :decimal then f(val.to_s.to_f)
|
74
|
-
else f(val)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
# Returns the ActiveRecord column type or nil.
|
79
|
-
def column_type(obj, attr)
|
80
|
-
column_property(obj, attr, :type)
|
81
|
-
end
|
82
|
-
|
83
|
-
# Returns an ActiveRecord column property for the passed attr or nil
|
84
|
-
def column_property(obj, attr, property)
|
85
|
-
if obj.respond_to?(:column_for_attribute)
|
86
|
-
column = obj.column_for_attribute(attr)
|
87
|
-
column.try(property)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
# Returns the :belongs_to association for the given attribute or nil if there is none.
|
92
|
-
def belongs_to_association(obj, attr)
|
93
|
-
if assoc = association(obj, attr)
|
94
|
-
assoc if assoc.macro == :belongs_to
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
# Returns the association proxy for the given attribute. The attr parameter
|
99
|
-
# may be the _id column or the association name. Returns nil if no association
|
100
|
-
# was found.
|
101
|
-
def association(obj, attr)
|
102
|
-
if obj.class.respond_to?(:reflect_on_association)
|
103
|
-
assoc = attr.to_s =~ /_id$/ ? attr.to_s[0..-4].to_sym : attr
|
104
|
-
obj.class.reflect_on_association(assoc)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
48
|
|
109
49
|
############## STANDARD HTML SECTIONS ############################
|
110
50
|
|
@@ -113,8 +53,7 @@ module StandardHelper
|
|
113
53
|
# Without block, this may be used in the form <%= labeled(...) %>, with like <% labeled(..) do %>
|
114
54
|
def labeled(label, content = nil, &block)
|
115
55
|
content = capture(&block) if block_given?
|
116
|
-
|
117
|
-
block_given? ? concat(html) : html
|
56
|
+
render(:partial => 'shared/labeled', :locals => { :label => label, :content => content})
|
118
57
|
end
|
119
58
|
|
120
59
|
# Transform the given text into a form as used by labels or table headers.
|
@@ -131,16 +70,20 @@ module StandardHelper
|
|
131
70
|
def render_attrs(obj, attrs, div = true)
|
132
71
|
html = attrs.collect do |a|
|
133
72
|
labeled(captionize(a, obj.class), format_attr(obj, a))
|
134
|
-
end.join
|
73
|
+
end.join.html_safe
|
135
74
|
|
136
75
|
div ? content_tag(:div, html, :class => 'attributes') : html
|
137
76
|
end
|
138
77
|
|
139
|
-
# Renders a table for the given entries
|
140
|
-
# If
|
141
|
-
|
78
|
+
# Renders a table for the given entries. One column is rendered for each attribute passed.
|
79
|
+
# If a block is given, the columns defined therein are appended to the attribute columns.
|
80
|
+
# If entries is empty, an appropriate message is rendered.
|
81
|
+
def table(entries, *attrs, &block)
|
142
82
|
if entries.present?
|
143
|
-
StandardTableBuilder.table(entries, self
|
83
|
+
StandardTableBuilder.table(entries, self) do |t|
|
84
|
+
t.attrs(*attrs)
|
85
|
+
yield t if block_given?
|
86
|
+
end
|
144
87
|
else
|
145
88
|
content_tag(:div, NO_LIST_ENTRIES_MESSAGE, :class => 'list')
|
146
89
|
end
|
@@ -150,20 +93,18 @@ module StandardHelper
|
|
150
93
|
# Before the input fields, the error messages are rendered, if present.
|
151
94
|
# The form is rendered with a basic save button.
|
152
95
|
# If a block is given, custom input fields may be rendered and attrs is ignored.
|
153
|
-
|
154
|
-
# The form is always directly printed into the erb, so the call must
|
155
|
-
# go within a normal <% form(...) %> section, not in a <%= output section
|
156
|
-
def standard_form(object, attrs = [], options = {})
|
96
|
+
def standard_form(object, attrs = [], options = {}, &block)
|
157
97
|
form_for(object, {:builder => StandardFormBuilder}.merge(options)) do |form|
|
158
|
-
|
98
|
+
content = ""
|
99
|
+
content << render(:partial => 'shared/error_messages', :locals => {:errors => object.errors})
|
159
100
|
|
160
|
-
if block_given?
|
161
|
-
|
101
|
+
content << if block_given?
|
102
|
+
capture(form, &block)
|
162
103
|
else
|
163
|
-
|
104
|
+
form.labeled_input_fields(*attrs)
|
164
105
|
end
|
165
106
|
|
166
|
-
|
107
|
+
content << labeled(nil, form.submit("Save"))
|
167
108
|
end
|
168
109
|
end
|
169
110
|
|
@@ -206,4 +147,68 @@ module StandardHelper
|
|
206
147
|
link_to("[#{label}]", options, {:class => 'action'}.merge(html_options))
|
207
148
|
end
|
208
149
|
|
150
|
+
protected
|
151
|
+
|
152
|
+
# Helper methods that are not necessarely called from templates.
|
153
|
+
|
154
|
+
# Formats an active record association
|
155
|
+
def format_assoc(obj, assoc)
|
156
|
+
if assoc_val = obj.send(assoc.name)
|
157
|
+
link_to_unless(no_assoc_link?(assoc), h(assoc_val.label), assoc_val)
|
158
|
+
else
|
159
|
+
'(none)'
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
# Returns true if no link should be created when formatting the given association.
|
164
|
+
def no_assoc_link?(assoc)
|
165
|
+
(respond_to?(:no_assoc_links) && no_assoc_links.to_a.include?(assoc.name.to_sym)) ||
|
166
|
+
!respond_to?("#{assoc.klass.name.underscore}_path".to_sym)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Formats an arbitrary attribute of the given object depending on its data type.
|
170
|
+
# For ActiveRecords, take the defined data type into account for special types
|
171
|
+
# that have no own object class.
|
172
|
+
def format_type(obj, attr)
|
173
|
+
val = obj.send(attr)
|
174
|
+
return EMPTY_STRING if val.nil?
|
175
|
+
case column_type(obj, attr)
|
176
|
+
when :time then val.strftime(TIME_FORMAT)
|
177
|
+
when :date then val.to_date.to_s
|
178
|
+
when :text then simple_format(h(val))
|
179
|
+
when :decimal then f(val.to_s.to_f)
|
180
|
+
else f(val)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
# Returns the ActiveRecord column type or nil.
|
185
|
+
def column_type(obj, attr)
|
186
|
+
column_property(obj, attr, :type)
|
187
|
+
end
|
188
|
+
|
189
|
+
# Returns an ActiveRecord column property for the passed attr or nil
|
190
|
+
def column_property(obj, attr, property)
|
191
|
+
if obj.respond_to?(:column_for_attribute)
|
192
|
+
column = obj.column_for_attribute(attr)
|
193
|
+
column.try(property)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
# Returns the :belongs_to association for the given attribute or nil if there is none.
|
198
|
+
def belongs_to_association(obj, attr)
|
199
|
+
if assoc = association(obj, attr)
|
200
|
+
assoc if assoc.macro == :belongs_to
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Returns the association proxy for the given attribute. The attr parameter
|
205
|
+
# may be the _id column or the association name. Returns nil if no association
|
206
|
+
# was found.
|
207
|
+
def association(obj, attr)
|
208
|
+
if obj.class.respond_to?(:reflect_on_association)
|
209
|
+
assoc = attr.to_s =~ /_id$/ ? attr.to_s[0..-4].to_sym : attr
|
210
|
+
obj.class.reflect_on_association(assoc)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
209
214
|
end
|
@@ -6,17 +6,17 @@
|
|
6
6
|
# t.attrs :name, :city
|
7
7
|
# end
|
8
8
|
class StandardTableBuilder
|
9
|
-
|
9
|
+
attr_reader :entries, :cols, :template
|
10
10
|
|
11
11
|
# Delegate called methods to template.
|
12
12
|
# including StandardHelper would lead to problems with indirectly called methods.
|
13
|
-
|
13
|
+
delegate :content_tag, :format_attr, :column_type, :belongs_to_association,
|
14
14
|
:captionize, :tr_alt, :to => :template
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def initialize(entries, template)
|
17
|
+
@entries = entries
|
18
|
+
@template = template
|
19
|
+
@cols = []
|
20
20
|
end
|
21
21
|
|
22
22
|
# Convenience method to directly generate a table. Renders a row for each entry in entries.
|
@@ -31,26 +31,25 @@ class StandardTableBuilder
|
|
31
31
|
# Define a column for the table with the given header, the html_options used for
|
32
32
|
# each td and a block rendering the contents of a cell for the current entry.
|
33
33
|
# The columns appear in the order they are defined.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
def col(header = '', html_options = {}, &block)
|
35
|
+
@cols << Col.new(header, html_options, @template, block)
|
36
|
+
end
|
37
|
+
|
38
38
|
# Convenience method to add one or more attribute columns.
|
39
39
|
# The attribute name will become the header, the cells will contain
|
40
40
|
# the formatted attribute value for the current entry.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
def attrs(*attrs)
|
42
|
+
attrs.each do |a|
|
43
|
+
col(captionize(a, entry_class), :class => align_class(a)) { |e| format_attr(e, a) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
47
|
# Renders the table as HTML.
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
48
|
+
def to_html
|
49
|
+
content_tag :table, :class => 'list' do
|
50
|
+
html_header + entries.collect { |e| html_row(e) }.join.html_safe
|
51
|
+
end
|
52
|
+
end
|
54
53
|
|
55
54
|
# Returns css classes used for alignment of the cell data.
|
56
55
|
# Based on the column type of the attribute.
|
@@ -66,38 +65,38 @@ class StandardTableBuilder
|
|
66
65
|
|
67
66
|
private
|
68
67
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
68
|
+
def html_header
|
69
|
+
content_tag :tr do
|
70
|
+
cols.collect { |c| c.html_header }.join
|
71
|
+
end
|
73
72
|
end
|
74
73
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
74
|
+
def html_row(entry)
|
75
|
+
tr_alt do
|
76
|
+
cols.collect { |c| c.html_cell(entry) }.join
|
77
|
+
end
|
79
78
|
end
|
80
79
|
|
81
80
|
def entry_class
|
82
81
|
entries.first.class
|
83
82
|
end
|
84
|
-
|
83
|
+
|
85
84
|
# Helper class to store column information.
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
85
|
+
class Col < Struct.new(:header, :html_options, :template, :block) #:nodoc:
|
86
|
+
|
87
|
+
delegate :content_tag, :to => :template
|
88
|
+
|
89
|
+
def content(entry)
|
90
|
+
block.call(entry)
|
91
|
+
end
|
92
|
+
|
93
|
+
def html_header
|
94
|
+
content_tag :th, header
|
95
|
+
end
|
96
|
+
|
97
|
+
def html_cell(entry)
|
98
|
+
content_tag :td, content(entry), html_options
|
99
|
+
end
|
101
100
|
|
102
101
|
end
|
103
102
|
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= crud_form %>
|
File without changes
|
@@ -67,25 +67,25 @@ a:visited.action {
|
|
67
67
|
color: #663333;
|
68
68
|
}
|
69
69
|
|
70
|
-
|
70
|
+
#error_explanation {
|
71
71
|
border: solid 1px #999999;
|
72
72
|
margin: 10pt;
|
73
73
|
padding: 5pt;
|
74
74
|
background-color: #FFBBBB;
|
75
75
|
}
|
76
76
|
|
77
|
-
|
77
|
+
#error_explanation h2 {
|
78
78
|
font-size: 12pt;
|
79
79
|
margin-top: 0pt;
|
80
80
|
}
|
81
81
|
|
82
|
-
|
82
|
+
#error_explanation ul {
|
83
83
|
margin-bottom: 5pt;
|
84
84
|
}
|
85
85
|
|
86
|
-
div.
|
86
|
+
.value div.field_with_errors {
|
87
87
|
background-color: #FFBBBB;
|
88
88
|
display: inline-block;
|
89
|
-
padding:
|
89
|
+
padding: 1px;
|
90
90
|
}
|
91
91
|
|
@@ -34,6 +34,7 @@ class CrudTestModelsController < CrudController #:nodoc:
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
# create callback methods that record the before/after callbacks
|
37
38
|
[:create, :update, :save, :destroy].each do |a|
|
38
39
|
callback = "before_#{a.to_s}"
|
39
40
|
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
@@ -41,10 +42,18 @@ class CrudTestModelsController < CrudController #:nodoc:
|
|
41
42
|
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
42
43
|
end
|
43
44
|
|
45
|
+
# create callback methods that record the before_render callbacks
|
46
|
+
[:index, :show, :new, :edit].each do |a|
|
47
|
+
callback = "before_render_#{a.to_s}"
|
48
|
+
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
49
|
+
end
|
50
|
+
|
51
|
+
# handle the called callbacks
|
44
52
|
def method_missing(sym, *args)
|
45
53
|
called_callback(sym.to_s[HANDLE_PREFIX.size..-1].to_sym) if sym.to_s.starts_with?(HANDLE_PREFIX)
|
46
54
|
end
|
47
55
|
|
56
|
+
# callback to redirect if @should_redirect is set
|
48
57
|
def possibly_redirect
|
49
58
|
redirect_to :action => 'index' if should_redirect && !performed?
|
50
59
|
!should_redirect
|
@@ -54,6 +63,7 @@ class CrudTestModelsController < CrudController #:nodoc:
|
|
54
63
|
@companions = CrudTestModel.all :conditions => {:human => true}
|
55
64
|
end
|
56
65
|
|
66
|
+
# records a callback
|
57
67
|
def called_callback(callback)
|
58
68
|
@called_callbacks ||= []
|
59
69
|
@called_callbacks << callback
|
@@ -61,10 +71,6 @@ class CrudTestModelsController < CrudController #:nodoc:
|
|
61
71
|
|
62
72
|
end
|
63
73
|
|
64
|
-
ActionController::Routing::Routes.draw do |map|
|
65
|
-
map.resources :crud_test_models
|
66
|
-
end
|
67
|
-
|
68
74
|
# A simple test helper to prepare the test database with a CrudTestModel model.
|
69
75
|
module CrudTestHelper
|
70
76
|
|
@@ -74,20 +80,22 @@ module CrudTestHelper
|
|
74
80
|
# Look at the source to view the column definition.
|
75
81
|
def setup_db
|
76
82
|
silence_stream(STDOUT) do
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
83
|
+
ActiveRecord::Base.connection.create_table :crud_test_models, :force => true do |t|
|
84
|
+
t.string :name, :null => false, :limit => 50
|
85
|
+
t.string :whatever
|
86
|
+
t.integer :children
|
87
|
+
t.integer :companion_id
|
88
|
+
t.float :rating
|
89
|
+
t.decimal :income, :precision => 14, :scale => 2
|
90
|
+
t.date :birthdate
|
91
|
+
t.boolean :human, :default => true
|
92
|
+
t.text :remarks
|
93
|
+
|
94
|
+
t.timestamps
|
89
95
|
end
|
90
96
|
end
|
97
|
+
|
98
|
+
CrudTestModel.reset_column_information
|
91
99
|
end
|
92
100
|
|
93
101
|
# Removes the crud_test_models table from the database.
|
@@ -121,4 +129,4 @@ module CrudTestHelper
|
|
121
129
|
:remarks => "#{c} #{c} #{c}\n" * 3)
|
122
130
|
end
|
123
131
|
|
124
|
-
end
|
132
|
+
end
|
@@ -39,8 +39,9 @@ module CrudControllerTestHelper
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def test_show_without_id_redirects_to_index
|
42
|
-
|
43
|
-
|
42
|
+
assert_raise(ActionController::RoutingError) do
|
43
|
+
get :show
|
44
|
+
end
|
44
45
|
end
|
45
46
|
|
46
47
|
def test_new
|
@@ -59,17 +60,6 @@ module CrudControllerTestHelper
|
|
59
60
|
assert_test_attrs_equal
|
60
61
|
end
|
61
62
|
|
62
|
-
def test_create_with_wrong_http_method_redirects
|
63
|
-
get :create, model_identifier => test_entry_attrs
|
64
|
-
assert_redirected_to_index
|
65
|
-
|
66
|
-
put :create, model_identifier => test_entry_attrs
|
67
|
-
assert_redirected_to_index
|
68
|
-
|
69
|
-
delete :create, model_identifier => test_entry_attrs
|
70
|
-
assert_redirected_to_index
|
71
|
-
end
|
72
|
-
|
73
63
|
def test_create_xml
|
74
64
|
assert_difference("#{model_class.name}.count") do
|
75
65
|
post :create, model_identifier => test_entry_attrs, :format => 'xml'
|
@@ -86,7 +76,7 @@ module CrudControllerTestHelper
|
|
86
76
|
end
|
87
77
|
|
88
78
|
def test_edit_without_id_raises_RecordNotFound
|
89
|
-
assert_raise(
|
79
|
+
assert_raise(ActionController::RoutingError) do
|
90
80
|
get :edit
|
91
81
|
end
|
92
82
|
end
|
@@ -98,15 +88,7 @@ module CrudControllerTestHelper
|
|
98
88
|
assert_test_attrs_equal
|
99
89
|
assert_redirected_to assigns(:entry)
|
100
90
|
end
|
101
|
-
|
102
|
-
def test_update_with_wrong_http_method_redirects
|
103
|
-
get :update, :id => test_entry.id, model_identifier => test_entry_attrs
|
104
|
-
assert_redirected_to_index
|
105
|
-
|
106
|
-
delete :update, :id => test_entry.id, model_identifier => test_entry_attrs
|
107
|
-
assert_redirected_to_index
|
108
|
-
end
|
109
|
-
|
91
|
+
|
110
92
|
def test_update_xml
|
111
93
|
assert_no_difference("#{model_class.name}.count") do
|
112
94
|
put :update, :id => test_entry.id, model_identifier => test_entry_attrs, :format => 'xml'
|
@@ -121,15 +103,7 @@ module CrudControllerTestHelper
|
|
121
103
|
end
|
122
104
|
assert_redirected_to_index
|
123
105
|
end
|
124
|
-
|
125
|
-
def test_destroy_with_wrong_http_method_redirects
|
126
|
-
get :destroy, :id => test_entry.id
|
127
|
-
assert_redirected_to_index
|
128
|
-
|
129
|
-
put :destroy, :id => test_entry.id
|
130
|
-
assert_redirected_to_index
|
131
|
-
end
|
132
|
-
|
106
|
+
|
133
107
|
def test_destroy_xml
|
134
108
|
assert_difference("#{model_class.name}.count", -1) do
|
135
109
|
delete :destroy, :id => test_entry.id, :format => 'xml'
|
@@ -138,6 +112,36 @@ module CrudControllerTestHelper
|
|
138
112
|
assert_equal "", @response.body.strip
|
139
113
|
end
|
140
114
|
|
115
|
+
# no need to test http methods for pure restfull controllers
|
116
|
+
def ignore_test_create_with_wrong_http_method_redirects
|
117
|
+
get :create, model_identifier => test_entry_attrs
|
118
|
+
assert_redirected_to_index
|
119
|
+
|
120
|
+
put :create, model_identifier => test_entry_attrs
|
121
|
+
assert_redirected_to_index
|
122
|
+
|
123
|
+
delete :create, model_identifier => test_entry_attrs
|
124
|
+
assert_redirected_to_index
|
125
|
+
end
|
126
|
+
|
127
|
+
# no need to test http methods for pure restfull controllers
|
128
|
+
def ignore_test_update_with_wrong_http_method_redirects
|
129
|
+
get :update, :id => test_entry.id, model_identifier => test_entry_attrs
|
130
|
+
assert_redirected_to_index
|
131
|
+
|
132
|
+
delete :update, :id => test_entry.id, model_identifier => test_entry_attrs
|
133
|
+
assert_redirected_to_index
|
134
|
+
end
|
135
|
+
|
136
|
+
# no need to test http methods for pure restfull controllers
|
137
|
+
def ignore_test_destroy_with_wrong_http_method_redirects
|
138
|
+
get :destroy, :id => test_entry.id
|
139
|
+
assert_redirected_to_index
|
140
|
+
|
141
|
+
put :destroy, :id => test_entry.id
|
142
|
+
assert_redirected_to_index
|
143
|
+
end
|
144
|
+
|
141
145
|
protected
|
142
146
|
|
143
147
|
def assert_redirected_to_index
|
@@ -9,16 +9,18 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
9
9
|
|
10
10
|
attr_accessor :models
|
11
11
|
|
12
|
-
setup :reset_db, :setup_db, :create_test_data
|
12
|
+
setup :reset_db, :setup_db, :create_test_data, :special_routing
|
13
13
|
|
14
14
|
teardown :reset_db
|
15
|
+
|
15
16
|
|
16
17
|
def test_setup
|
17
18
|
assert_equal 6, CrudTestModel.count
|
18
19
|
assert_equal CrudTestModelsController, @controller.class
|
19
20
|
assert_recognizes({:controller => 'crud_test_models', :action => 'index'}, '/crud_test_models')
|
20
21
|
assert_recognizes({:controller => 'crud_test_models', :action => 'show', :id => '1'}, '/crud_test_models/1')
|
21
|
-
|
22
|
+
# no need to hide actions for pure restful controllers
|
23
|
+
#assert_equal %w(index show new create edit update destroy).to_set, CrudTestModelsController.send(:action_methods)
|
22
24
|
end
|
23
25
|
|
24
26
|
def test_index
|
@@ -56,7 +58,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
56
58
|
assert assigns(:companions)
|
57
59
|
assert flash[:error].present?
|
58
60
|
assert_equal 'illegal', assigns(:entry).name
|
59
|
-
|
61
|
+
assert_equal [:before_render_new], @controller.called_callbacks
|
60
62
|
end
|
61
63
|
|
62
64
|
def test_create_with_before_callback_redirect
|
@@ -75,9 +77,20 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
75
77
|
assert_nil assigns(:companions)
|
76
78
|
end
|
77
79
|
|
78
|
-
|
79
80
|
protected
|
80
81
|
|
82
|
+
def special_routing
|
83
|
+
@routes = ActionDispatch::Routing::RouteSet.new
|
84
|
+
_routes = @routes
|
85
|
+
|
86
|
+
@controller.singleton_class.send(:include, _routes.url_helpers)
|
87
|
+
@controller.view_context_class = Class.new(@controller.view_context_class) do
|
88
|
+
include _routes.url_helpers
|
89
|
+
end
|
90
|
+
|
91
|
+
@routes.draw { resources :crud_test_models }
|
92
|
+
end
|
93
|
+
|
81
94
|
def test_entry
|
82
95
|
crud_test_models(:AAAAA)
|
83
96
|
end
|