dry_crud 1.2.7 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +60 -27
- data/Rakefile +3 -1
- data/VERSION +1 -1
- data/lib/generators/dry_crud/dry_crud_generator.rb +3 -3
- data/lib/generators/dry_crud/templates/INSTALL +3 -1
- data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +106 -90
- data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +90 -74
- data/lib/generators/dry_crud/templates/app/controllers/render_inheritable.rb +34 -33
- data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +39 -23
- data/lib/generators/dry_crud/templates/app/helpers/list_helper.rb +11 -9
- data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +55 -47
- data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +134 -86
- data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +41 -35
- data/lib/generators/dry_crud/templates/app/views/crud/_actions_edit.html.erb +1 -0
- data/lib/generators/dry_crud/templates/app/views/crud/edit.html.erb +3 -3
- data/lib/generators/dry_crud/templates/app/views/crud/new.html.erb +2 -2
- data/lib/generators/dry_crud/templates/app/views/crud/show.html.erb +3 -3
- data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +9 -7
- data/lib/generators/dry_crud/templates/app/views/list/_search.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/list/index.html.erb +4 -4
- data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.erb +3 -1
- data/lib/generators/dry_crud/templates/config/locales/en_crud.yml +63 -0
- data/lib/generators/dry_crud/templates/test/crud_test_model.rb +93 -58
- data/lib/generators/dry_crud/templates/test/custom_assertions.rb +24 -13
- data/lib/generators/dry_crud/templates/test/functional/crud_controller_test_helper.rb +26 -56
- data/lib/generators/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +47 -41
- data/lib/generators/dry_crud/templates/test/unit/custom_assertions_test.rb +28 -24
- data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +20 -34
- data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +39 -53
- data/lib/generators/dry_crud/templates/test/unit/helpers/render_inheritable_test.rb +33 -33
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_form_builder_test.rb +27 -27
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +103 -50
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_table_builder_test.rb +52 -24
- data/test/templates/Gemfile +34 -0
- data/test/templates/app/controllers/ajax_controller.rb +3 -3
- data/test/templates/app/controllers/application_controller.rb +1 -1
- data/test/templates/app/controllers/cities_controller.rb +2 -5
- data/test/templates/app/controllers/people_controller.rb +5 -5
- data/test/templates/app/controllers/vips_controller.rb +6 -11
- data/test/templates/app/helpers/people_helper.rb +2 -2
- data/test/templates/app/models/city.rb +9 -9
- data/test/templates/app/models/person.rb +5 -4
- data/test/templates/app/views/ajax/_actions_index.html.erb +2 -2
- data/test/templates/app/views/cities/_form.html.erb +5 -1
- data/test/templates/app/views/layouts/_menu.html.erb +3 -3
- data/test/templates/app/views/people/_attrs.html.erb +3 -3
- data/test/templates/config/database.yml +22 -0
- data/test/templates/config/locales/en_cities.yml +56 -0
- data/test/templates/config/routes.rb +5 -5
- data/test/templates/db/migrate/20100511174904_create_people_and_cities.rb +5 -2
- data/test/templates/db/seeds.rb +38 -29
- data/test/templates/test/functional/cities_controller_test.rb +12 -12
- data/test/templates/test/functional/people_controller_test.rb +10 -10
- metadata +11 -7
@@ -6,24 +6,25 @@
|
|
6
6
|
# t.attrs :name, :city
|
7
7
|
# end
|
8
8
|
class StandardTableBuilder
|
9
|
-
attr_reader :entries, :cols, :template
|
10
|
-
|
9
|
+
attr_reader :entries, :cols, :options, :template
|
10
|
+
|
11
11
|
# Delegate called methods to template.
|
12
12
|
# including StandardHelper would lead to problems with indirectly called methods.
|
13
|
-
delegate :content_tag, :format_attr, :column_type, :association,
|
13
|
+
delegate :content_tag, :format_attr, :column_type, :association,
|
14
14
|
:captionize, :tr_alt, :to => :template
|
15
15
|
|
16
|
-
def initialize(entries, template)
|
16
|
+
def initialize(entries, template, options = {})
|
17
17
|
@entries = entries
|
18
18
|
@template = template
|
19
|
+
@options = options
|
19
20
|
@cols = []
|
20
21
|
end
|
21
22
|
|
22
23
|
# Convenience method to directly generate a table. Renders a row for each entry in entries.
|
23
24
|
# Takes a block that gets the table object as parameter for configuration.
|
24
25
|
# Returns the generated html for the table.
|
25
|
-
def self.table(entries, template)
|
26
|
-
t = new(entries, template)
|
26
|
+
def self.table(entries, template, options = {})
|
27
|
+
t = new(entries, template, options)
|
27
28
|
yield t
|
28
29
|
t.to_html
|
29
30
|
end
|
@@ -34,7 +35,7 @@ class StandardTableBuilder
|
|
34
35
|
def col(header = '', html_options = {}, &block)
|
35
36
|
@cols << Col.new(header, html_options, @template, block)
|
36
37
|
end
|
37
|
-
|
38
|
+
|
38
39
|
# Convenience method to add one or more attribute columns.
|
39
40
|
# The attribute name will become the header, the cells will contain
|
40
41
|
# the formatted attribute value for the current entry.
|
@@ -43,7 +44,7 @@ class StandardTableBuilder
|
|
43
44
|
attr(a)
|
44
45
|
end
|
45
46
|
end
|
46
|
-
|
47
|
+
|
47
48
|
# Define a column for the given attribute and an optional header.
|
48
49
|
# If no header is given, the attribute name is used. The cell will
|
49
50
|
# contain the formatted attribute value for the current entry.
|
@@ -51,41 +52,42 @@ class StandardTableBuilder
|
|
51
52
|
header ||= attr_header(a)
|
52
53
|
col(header, :class => align_class(a)) { |e| format_attr(e, a) }
|
53
54
|
end
|
54
|
-
|
55
|
+
|
55
56
|
# Renders the table as HTML.
|
56
57
|
def to_html
|
57
58
|
content_tag :table, :class => 'list' do
|
58
|
-
html_header +
|
59
|
+
content_tag(:thead, html_header) +
|
60
|
+
content_tag(:tbody, safe_join(entries) { |e| html_row(e) })
|
59
61
|
end
|
60
62
|
end
|
61
|
-
|
63
|
+
|
62
64
|
# Returns css classes used for alignment of the cell data.
|
63
65
|
# Based on the column type of the attribute.
|
64
66
|
def align_class(attr)
|
65
67
|
entry = entries.first
|
66
68
|
case column_type(entry, attr)
|
67
|
-
when :integer, :float, :decimal
|
69
|
+
when :integer, :float, :decimal
|
68
70
|
'right' unless association(entry, attr, :belongs_to)
|
69
|
-
when :boolean
|
71
|
+
when :boolean
|
70
72
|
'center'
|
71
73
|
end
|
72
74
|
end
|
73
|
-
|
75
|
+
|
74
76
|
def attr_header(attr)
|
75
77
|
captionize(attr, entry_class)
|
76
78
|
end
|
77
|
-
|
78
|
-
private
|
79
|
-
|
79
|
+
|
80
|
+
private
|
81
|
+
|
80
82
|
def html_header
|
81
83
|
content_tag :tr do
|
82
|
-
cols
|
84
|
+
safe_join(cols) { |c| c.html_header }
|
83
85
|
end
|
84
86
|
end
|
85
87
|
|
86
88
|
def html_row(entry)
|
87
89
|
tr_alt do
|
88
|
-
cols
|
90
|
+
safe_join(cols) { |c| c.html_cell(entry) }
|
89
91
|
end
|
90
92
|
end
|
91
93
|
|
@@ -93,26 +95,30 @@ class StandardTableBuilder
|
|
93
95
|
entries.first.class
|
94
96
|
end
|
95
97
|
|
98
|
+
def safe_join(collection, &block)
|
99
|
+
collection.collect(&block).join.html_safe
|
100
|
+
end
|
101
|
+
|
96
102
|
# Helper class to store column information.
|
97
103
|
class Col < Struct.new(:header, :html_options, :template, :block) #:nodoc:
|
98
|
-
|
104
|
+
|
99
105
|
delegate :content_tag, :to => :template
|
100
|
-
|
106
|
+
|
101
107
|
def content(entry)
|
102
108
|
block.call(entry)
|
103
109
|
end
|
104
|
-
|
110
|
+
|
105
111
|
def html_header
|
106
112
|
content_tag :th, header
|
107
113
|
end
|
108
|
-
|
114
|
+
|
109
115
|
def html_cell(entry)
|
110
116
|
content_tag :td, content(entry), html_options
|
111
117
|
end
|
112
118
|
|
113
119
|
end
|
114
|
-
|
115
|
-
# Provides headers with sort links. Expects a method :sortable?(attr)
|
120
|
+
|
121
|
+
# Provides headers with sort links. Expects a method :sortable?(attr)
|
116
122
|
# in the template/controller to tell if an attribute is sortable or not.
|
117
123
|
# Extracted into an own module for convenience.
|
118
124
|
module Sorting
|
@@ -121,7 +127,7 @@ class StandardTableBuilder
|
|
121
127
|
label ||= attr_header(attr)
|
122
128
|
template.link_to(label, sort_params(attr)) + current_mark(attr)
|
123
129
|
end
|
124
|
-
|
130
|
+
|
125
131
|
# Same as :attrs, except that it renders a sort link in the header
|
126
132
|
# if an attr is sortable.
|
127
133
|
def sortable_attrs(*attrs)
|
@@ -129,44 +135,44 @@ class StandardTableBuilder
|
|
129
135
|
template.sortable?(a) ? sortable_attr(a) : attr(a)
|
130
136
|
end
|
131
137
|
end
|
132
|
-
|
138
|
+
|
133
139
|
# Renders a sort link header, otherwise similar to :attr.
|
134
140
|
def sortable_attr(a, header = nil)
|
135
141
|
attr(a, sort_header(a, header))
|
136
142
|
end
|
137
|
-
|
143
|
+
|
138
144
|
private
|
139
|
-
|
145
|
+
|
140
146
|
# Request params for the sort link.
|
141
147
|
def sort_params(attr)
|
142
148
|
params.merge({:sort => attr, :sort_dir => sort_dir(attr)})
|
143
149
|
end
|
144
|
-
|
150
|
+
|
145
151
|
# The sort mark, if any, for the given attribute.
|
146
152
|
def current_mark(attr)
|
147
|
-
if current_sort?(attr)
|
153
|
+
if current_sort?(attr)
|
148
154
|
(sort_dir(attr) == 'asc' ? ' ↑' : ' ↓').html_safe
|
149
155
|
else
|
150
156
|
''
|
151
157
|
end
|
152
158
|
end
|
153
|
-
|
159
|
+
|
154
160
|
# Returns true if the given attribute is the current sort column.
|
155
161
|
def current_sort?(attr)
|
156
162
|
params[:sort] == attr.to_s
|
157
163
|
end
|
158
|
-
|
164
|
+
|
159
165
|
# The sort direction to use in the sort link for the given attribute.
|
160
166
|
def sort_dir(attr)
|
161
167
|
current_sort?(attr) && params[:sort_dir] == 'asc' ? 'desc' : 'asc'
|
162
168
|
end
|
163
|
-
|
169
|
+
|
164
170
|
# Delegate to template.
|
165
171
|
def params
|
166
172
|
template.params
|
167
173
|
end
|
168
174
|
end
|
169
|
-
|
175
|
+
|
170
176
|
include Sorting
|
171
177
|
|
172
178
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
<% @title ||=
|
1
|
+
<% @title ||= ti(:title, :model => full_entry_label).html_safe -%>
|
2
2
|
|
3
|
-
<% content_for(:actions, render(
|
3
|
+
<% content_for(:actions, render('actions_edit')) %>
|
4
4
|
|
5
5
|
<%= clear %>
|
6
6
|
|
7
|
-
<%= render
|
7
|
+
<%= render 'form' %>
|
@@ -1,9 +1,9 @@
|
|
1
|
-
<% @title ||= full_entry_label -%>
|
1
|
+
<% @title ||= ti(:title, :model => full_entry_label).html_safe -%>
|
2
2
|
|
3
|
-
<% content_for(:actions, render(
|
3
|
+
<% content_for(:actions, render('actions_show')) %>
|
4
4
|
|
5
5
|
<%= clear %>
|
6
6
|
|
7
|
-
<%= render
|
7
|
+
<%= render 'attrs' %>
|
8
8
|
|
9
9
|
|
@@ -4,29 +4,28 @@
|
|
4
4
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
5
|
<head>
|
6
6
|
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
7
|
-
<title><%= @title %></title>
|
7
|
+
<title><%= strip_tags(@title) %></title>
|
8
8
|
<%= stylesheet_link_tag 'crud' %>
|
9
|
-
<%= javascript_include_tag :all %>
|
10
9
|
<%= csrf_meta_tag %>
|
11
|
-
<%= yield :head %>
|
10
|
+
<%= yield :head %>
|
12
11
|
</head>
|
13
12
|
<body>
|
14
13
|
|
15
14
|
<div id="container">
|
16
15
|
<ul id="menu">
|
17
|
-
<%= render
|
16
|
+
<%= render 'layouts/menu' %>
|
18
17
|
</ul>
|
19
18
|
|
20
19
|
<h1><%= @title %></h1>
|
21
20
|
|
22
21
|
<% if flash.notice.present? %>
|
23
|
-
<div id="flash_notice"><%= flash.notice %></div>
|
22
|
+
<div id="flash_notice"><%= raw(flash.notice) %></div>
|
24
23
|
<% elsif flash.alert.present? %>
|
25
|
-
<div id="flash_alert"><%= flash.alert %></div>
|
24
|
+
<div id="flash_alert"><%= raw(flash.alert) %></div>
|
26
25
|
<% end %>
|
27
26
|
|
28
27
|
<div class="actions">
|
29
|
-
<%= yield :actions %>
|
28
|
+
<%= yield :actions %>
|
30
29
|
</div>
|
31
30
|
|
32
31
|
<div id="content">
|
@@ -34,5 +33,8 @@
|
|
34
33
|
</div>
|
35
34
|
</div>
|
36
35
|
|
36
|
+
<%= javascript_include_tag :defaults, :cache => 'all' %>
|
37
|
+
<%= javascript_tag content_tag_for(:javascripts) if content_for?(:javascripts) %>
|
38
|
+
|
37
39
|
</body>
|
38
40
|
</html>
|
@@ -1,9 +1,9 @@
|
|
1
|
-
<% @title ||=
|
1
|
+
<% @title ||= ti(:title, :models => models_label) -%>
|
2
2
|
|
3
|
-
<% content_for(:actions, render(
|
3
|
+
<% content_for(:actions, render('actions_index')) %>
|
4
4
|
|
5
|
-
<%= render
|
5
|
+
<%= render 'search' if search_support? %>
|
6
6
|
|
7
7
|
<%= clear %>
|
8
8
|
|
9
|
-
<%= render
|
9
|
+
<%= render 'list' %>
|
@@ -1,6 +1,8 @@
|
|
1
1
|
<% if errors.any? %>
|
2
2
|
<div id="error_explanation">
|
3
|
-
<h2
|
3
|
+
<h2>
|
4
|
+
<%= ti(:"errors.header", :count => errors.count, :model => object.to_s) %>
|
5
|
+
</h2>
|
4
6
|
<ul>
|
5
7
|
<% errors.full_messages.each do |msg| %>
|
6
8
|
<li><%= msg %></li>
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# Translations of all crud strings.
|
2
|
+
# See also StandardHelper#translate_inheritable and #translate_association.
|
3
|
+
|
4
|
+
en:
|
5
|
+
# global scope
|
6
|
+
global:
|
7
|
+
"yes": "yes"
|
8
|
+
"no": "no"
|
9
|
+
no_list_entries: No entries found.
|
10
|
+
confirm_delete: Do you really want to delete this entry?
|
11
|
+
|
12
|
+
associations:
|
13
|
+
# association keys may be customized per model with the prefix
|
14
|
+
# 'activerecord.associations.{model}.' or even per actual association with
|
15
|
+
# 'activerecord.associations.models.{holder_model}.{assoc_name}.'
|
16
|
+
no_entry: (none)
|
17
|
+
none_available: (none available)
|
18
|
+
please_select: Please select
|
19
|
+
|
20
|
+
button:
|
21
|
+
save: Save
|
22
|
+
cancel: Cancel
|
23
|
+
search: Search
|
24
|
+
|
25
|
+
link:
|
26
|
+
show: Show
|
27
|
+
edit: Edit
|
28
|
+
add: Add
|
29
|
+
delete: Delete
|
30
|
+
list: List
|
31
|
+
|
32
|
+
errors:
|
33
|
+
header:
|
34
|
+
one: "1 error prohibited this entry from being saved:"
|
35
|
+
other: "%{count} errors prohibited this entry from being saved:"
|
36
|
+
|
37
|
+
# formats
|
38
|
+
time:
|
39
|
+
formats:
|
40
|
+
time: "%H:%M"
|
41
|
+
|
42
|
+
# list controller
|
43
|
+
list:
|
44
|
+
index:
|
45
|
+
title: Listing %{models}
|
46
|
+
|
47
|
+
# crud controller
|
48
|
+
crud:
|
49
|
+
show:
|
50
|
+
title: %{model}
|
51
|
+
new:
|
52
|
+
title: New %{model}
|
53
|
+
edit:
|
54
|
+
title: Edit %{model}
|
55
|
+
create:
|
56
|
+
flash:
|
57
|
+
success: %{model} was successfully created.
|
58
|
+
update:
|
59
|
+
flash:
|
60
|
+
success: %{model} was successfully updated.
|
61
|
+
destroy:
|
62
|
+
flash:
|
63
|
+
success: %{model} was successfully deleted.
|
@@ -2,55 +2,62 @@
|
|
2
2
|
class CrudTestModel < ActiveRecord::Base #:nodoc:
|
3
3
|
|
4
4
|
belongs_to :companion, :class_name => 'CrudTestModel'
|
5
|
-
|
5
|
+
|
6
6
|
validates :name, :presence => true
|
7
7
|
validates :rating, :inclusion => { :in => 1..10 }
|
8
|
-
|
9
|
-
default_scope order('name')
|
10
|
-
|
11
|
-
def
|
8
|
+
|
9
|
+
default_scope order('name')
|
10
|
+
|
11
|
+
def to_s
|
12
12
|
name
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def chatty
|
16
|
-
|
16
|
+
remarks.size
|
17
17
|
end
|
18
|
+
|
18
19
|
end
|
19
20
|
|
20
21
|
# Controller for the dummy model.
|
21
22
|
class CrudTestModelsController < CrudController #:nodoc:
|
22
23
|
HANDLE_PREFIX = 'handle_'
|
23
|
-
|
24
|
+
|
24
25
|
self.search_columns = [:name, :whatever, :remarks]
|
25
26
|
self.sort_mappings = {:chatty => 'length(remarks)'}
|
26
|
-
|
27
|
+
|
27
28
|
before_create :possibly_redirect
|
28
29
|
before_create :handle_name
|
29
|
-
|
30
|
+
|
30
31
|
before_render_new :possibly_redirect
|
31
32
|
before_render_new :set_companions
|
32
|
-
|
33
|
+
|
33
34
|
attr_reader :called_callbacks
|
34
35
|
attr_accessor :should_redirect
|
35
|
-
|
36
|
+
|
36
37
|
hide_action :called_callbacks, :should_redirect, :should_redirect=
|
37
|
-
|
38
|
+
|
38
39
|
# don't use the standard layout as it may require different routes
|
39
40
|
# than just the test route for this controller
|
40
41
|
layout nil
|
41
42
|
|
43
|
+
def destroy
|
44
|
+
super do |success, format|
|
45
|
+
format.html { redirect_to_index :notice => 'model is gone' } if success
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
42
49
|
protected
|
43
|
-
|
50
|
+
|
44
51
|
def list_entries
|
45
52
|
entries = super
|
46
|
-
|
47
|
-
|
53
|
+
if params[:filter]
|
54
|
+
entries = entries.where(['rating < ?', 3]).except(:order).order('children DESC')
|
48
55
|
end
|
49
56
|
entries
|
50
57
|
end
|
51
|
-
|
58
|
+
|
52
59
|
private
|
53
|
-
|
60
|
+
|
54
61
|
# custom callback
|
55
62
|
def handle_name
|
56
63
|
if @entry.name == 'illegal'
|
@@ -58,7 +65,7 @@ class CrudTestModelsController < CrudController #:nodoc:
|
|
58
65
|
return false
|
59
66
|
end
|
60
67
|
end
|
61
|
-
|
68
|
+
|
62
69
|
# create callback methods that record the before/after callbacks
|
63
70
|
[:create, :update, :save, :destroy].each do |a|
|
64
71
|
callback = "before_#{a.to_s}"
|
@@ -66,34 +73,34 @@ class CrudTestModelsController < CrudController #:nodoc:
|
|
66
73
|
callback = "after_#{a.to_s}"
|
67
74
|
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
68
75
|
end
|
69
|
-
|
76
|
+
|
70
77
|
# create callback methods that record the before_render callbacks
|
71
78
|
[:index, :show, :new, :edit, :form].each do |a|
|
72
79
|
callback = "before_render_#{a.to_s}"
|
73
80
|
send(callback.to_sym, :"#{HANDLE_PREFIX}#{callback}")
|
74
81
|
end
|
75
|
-
|
82
|
+
|
76
83
|
# handle the called callbacks
|
77
84
|
def method_missing(sym, *args)
|
78
85
|
called_callback(sym.to_s[HANDLE_PREFIX.size..-1].to_sym) if sym.to_s.starts_with?(HANDLE_PREFIX)
|
79
86
|
end
|
80
|
-
|
87
|
+
|
81
88
|
# callback to redirect if @should_redirect is set
|
82
89
|
def possibly_redirect
|
83
90
|
redirect_to :action => 'index' if should_redirect && !performed?
|
84
91
|
!should_redirect
|
85
92
|
end
|
86
|
-
|
93
|
+
|
87
94
|
def set_companions
|
88
95
|
@companions = CrudTestModel.all :conditions => {:human => true}
|
89
96
|
end
|
90
|
-
|
97
|
+
|
91
98
|
# records a callback
|
92
99
|
def called_callback(callback)
|
93
100
|
@called_callbacks ||= []
|
94
101
|
@called_callbacks << callback
|
95
102
|
end
|
96
|
-
|
103
|
+
|
97
104
|
end
|
98
105
|
|
99
106
|
# A simple test helper to prepare the test database with a CrudTestModel model.
|
@@ -101,53 +108,77 @@ end
|
|
101
108
|
# without the need for an application based model.
|
102
109
|
module CrudTestHelper
|
103
110
|
|
104
|
-
|
111
|
+
# Controller helper methods for the tests
|
112
|
+
|
113
|
+
def model_class
|
114
|
+
CrudTestModel
|
115
|
+
end
|
116
|
+
|
117
|
+
def controller_name
|
118
|
+
'crud_test_models'
|
119
|
+
end
|
120
|
+
|
121
|
+
def action_name
|
122
|
+
'index'
|
123
|
+
end
|
124
|
+
|
125
|
+
def params
|
126
|
+
{}
|
127
|
+
end
|
128
|
+
|
129
|
+
def sortable?(attr)
|
130
|
+
true
|
131
|
+
end
|
105
132
|
|
133
|
+
protected
|
134
|
+
|
106
135
|
# Sets up the test database with a crud_test_models table.
|
107
136
|
# Look at the source to view the column definition.
|
108
|
-
def setup_db
|
137
|
+
def setup_db
|
109
138
|
without_transaction do
|
110
139
|
silence_stream(STDOUT) do
|
111
140
|
ActiveRecord::Base.connection.create_table :crud_test_models, :force => true do |t|
|
112
|
-
t.string
|
113
|
-
t.string
|
114
|
-
t.string
|
115
|
-
t.integer
|
116
|
-
t.integer
|
117
|
-
t.float
|
118
|
-
t.decimal
|
119
|
-
t.date
|
120
|
-
t.
|
121
|
-
t.
|
122
|
-
|
141
|
+
t.string :name, :null => false, :limit => 50
|
142
|
+
t.string :password
|
143
|
+
t.string :whatever
|
144
|
+
t.integer :children
|
145
|
+
t.integer :companion_id
|
146
|
+
t.float :rating
|
147
|
+
t.decimal :income, :precision => 14, :scale => 2
|
148
|
+
t.date :birthdate
|
149
|
+
t.time :gets_up_at
|
150
|
+
t.datetime :last_seen
|
151
|
+
t.boolean :human, :default => true
|
152
|
+
t.text :remarks
|
153
|
+
|
123
154
|
t.timestamps
|
124
155
|
end
|
125
156
|
end
|
126
|
-
|
157
|
+
|
127
158
|
CrudTestModel.reset_column_information
|
128
159
|
end
|
129
160
|
end
|
130
|
-
|
161
|
+
|
131
162
|
# Removes the crud_test_models table from the database.
|
132
163
|
def reset_db
|
133
164
|
c = ActiveRecord::Base.connection
|
134
|
-
[:crud_test_models].each do |table|
|
165
|
+
[:crud_test_models].each do |table|
|
135
166
|
if c.table_exists?(table)
|
136
167
|
c.drop_table(table) rescue nil
|
137
168
|
end
|
138
169
|
end
|
139
170
|
end
|
140
|
-
|
171
|
+
|
141
172
|
# Creates 6 dummy entries for the crud_test_models table.
|
142
173
|
def create_test_data
|
143
174
|
(1..6).inject(nil) {|prev, i| create(i, prev) }
|
144
175
|
end
|
145
|
-
|
176
|
+
|
146
177
|
# Fixture-style accessor method to get CrudTestModel instances by name
|
147
178
|
def crud_test_models(name)
|
148
179
|
CrudTestModel.find_by_name(name.to_s)
|
149
180
|
end
|
150
|
-
|
181
|
+
|
151
182
|
def with_test_routing
|
152
183
|
with_routing do |set|
|
153
184
|
set.draw { resources :crud_test_models }
|
@@ -156,26 +187,30 @@ module CrudTestHelper
|
|
156
187
|
yield
|
157
188
|
end
|
158
189
|
end
|
159
|
-
|
160
|
-
|
190
|
+
|
161
191
|
private
|
162
|
-
|
192
|
+
|
163
193
|
def create(index, companion)
|
164
194
|
c = str(index)
|
165
|
-
CrudTestModel.create!(:name => c,
|
166
|
-
:children => 10 - index,
|
195
|
+
CrudTestModel.create!(:name => c,
|
196
|
+
:children => 10 - index,
|
167
197
|
:companion => companion,
|
168
|
-
:rating => "#{index}.#{index}".to_f,
|
169
|
-
:income => 10000000 * index + 0.1 * index,
|
170
|
-
:birthdate => "#{1900 + 10 * index}-#{index}-#{index}",
|
198
|
+
:rating => "#{index}.#{index}".to_f,
|
199
|
+
:income => 10000000 * index + 0.1 * index,
|
200
|
+
:birthdate => "#{1900 + 10 * index}-#{index}-#{index}",
|
201
|
+
# store entire date to avoid time zone issues
|
202
|
+
:gets_up_at => RUBY_VERSION.include?('1.9.') ?
|
203
|
+
Time.local(2000,1,1,index,index) :
|
204
|
+
Time.utc(2000,1,1,index,index),
|
205
|
+
:last_seen => "#{2000 + 10 * index}-#{index}-#{index} 1#{index}:2#{index}",
|
171
206
|
:human => index % 2 == 0,
|
172
207
|
:remarks => "#{c} #{str(index + 1)} #{str(index + 2)}\n" * (index % 3 + 1))
|
173
208
|
end
|
174
|
-
|
209
|
+
|
175
210
|
def str(index)
|
176
211
|
(index + 64).chr * 5
|
177
212
|
end
|
178
|
-
|
213
|
+
|
179
214
|
# hack to avoid ddl in transaction issues with mysql.
|
180
215
|
def without_transaction
|
181
216
|
c = ActiveRecord::Base.connection
|
@@ -185,10 +220,10 @@ module CrudTestHelper
|
|
185
220
|
c.execute("ROLLBACK")
|
186
221
|
start_transaction = true
|
187
222
|
end
|
188
|
-
|
189
|
-
yield
|
190
|
-
|
223
|
+
|
224
|
+
yield
|
225
|
+
|
191
226
|
c.execute("BEGIN") if start_transaction
|
192
227
|
end
|
193
|
-
|
228
|
+
|
194
229
|
end
|