dry_crud 3.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +1 -1
  3. data/VERSION +1 -1
  4. data/app/controllers/crud_controller.rb +10 -8
  5. data/app/controllers/dry_crud/generic_model.rb +6 -4
  6. data/app/controllers/dry_crud/nestable.rb +9 -11
  7. data/app/controllers/dry_crud/rememberable.rb +5 -1
  8. data/app/controllers/dry_crud/render_callbacks.rb +40 -19
  9. data/app/controllers/dry_crud/searchable.rb +40 -31
  10. data/app/controllers/dry_crud/sortable.rb +36 -26
  11. data/app/controllers/list_controller.rb +2 -2
  12. data/app/helpers/dry_crud/form/builder.rb +21 -13
  13. data/app/helpers/dry_crud/form/control.rb +8 -9
  14. data/app/helpers/dry_crud/table/actions.rb +6 -1
  15. data/app/helpers/dry_crud/table/builder.rb +4 -2
  16. data/app/helpers/dry_crud/table/col.rb +12 -1
  17. data/app/helpers/dry_crud/table/sorting.rb +7 -1
  18. data/app/helpers/form_helper.rb +7 -7
  19. data/app/helpers/format_helper.rb +3 -3
  20. data/app/helpers/i18n_helper.rb +3 -3
  21. data/app/helpers/utility_helper.rb +4 -1
  22. data/lib/dry_crud.rb +1 -2
  23. data/lib/dry_crud/engine.rb +5 -7
  24. data/lib/generators/dry_crud/dry_crud_generator.rb +3 -4
  25. data/lib/generators/dry_crud/dry_crud_generator_base.rb +6 -8
  26. data/lib/generators/dry_crud/file_generator.rb +1 -3
  27. data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +8 -18
  28. data/lib/generators/dry_crud/templates/spec/helpers/form_helper_spec.rb +1 -1
  29. data/lib/generators/dry_crud/templates/spec/helpers/utility_helper_spec.rb +4 -4
  30. data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +7 -17
  31. data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +13 -29
  32. data/lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb +25 -44
  33. data/lib/generators/dry_crud/templates/test/helpers/custom_assertions_test.rb +0 -1
  34. data/lib/generators/dry_crud/templates/test/helpers/dry_crud/form/builder_test.rb +3 -4
  35. data/lib/generators/dry_crud/templates/test/helpers/dry_crud/table/builder_test.rb +0 -2
  36. data/lib/generators/dry_crud/templates/test/helpers/form_helper_test.rb +2 -1
  37. data/lib/generators/dry_crud/templates/test/helpers/format_helper_test.rb +1 -1
  38. data/lib/generators/dry_crud/templates/test/helpers/table_helper_test.rb +8 -17
  39. data/lib/generators/dry_crud/templates/test/helpers/utility_helper_test.rb +3 -0
  40. data/lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb +27 -30
  41. data/lib/generators/dry_crud/templates/test/support/crud_test_helper.rb +11 -13
  42. data/lib/generators/dry_crud/templates/test/support/crud_test_model.rb +7 -3
  43. data/lib/generators/dry_crud/templates/test/support/crud_test_models_controller.rb +5 -9
  44. data/lib/generators/dry_crud/templates/test/support/custom_assertions.rb +1 -1
  45. metadata +10 -11
@@ -8,7 +8,6 @@ require 'support/crud_test_model'
8
8
  class CustomAssertionsTest < ActiveSupport::TestCase
9
9
 
10
10
  include CustomAssertions
11
-
12
11
  include CrudTestHelper
13
12
 
14
13
  setup :reset_db, :setup_db, :create_test_data
@@ -6,7 +6,6 @@ module DryCrud
6
6
  module Form
7
7
  # Test DryCrud::Form::Builder
8
8
  class BuilderTest < ActionView::TestCase
9
-
10
9
  include FormatHelper
11
10
  include I18nHelper
12
11
  include CrudTestHelper
@@ -26,9 +25,9 @@ module DryCrud
26
25
 
27
26
  test 'input_field dispatches string attr to string_field' do
28
27
  assert_equal form.with_addon(
29
- form.string_field(:name,
30
- required: 'required'),
31
- '*'),
28
+ form.string_field(:name,
29
+ required: 'required'),
30
+ '*'),
32
31
  form.input_field(:name)
33
32
  assert form.string_field(:name).html_safe?
34
33
  end
@@ -5,7 +5,6 @@ module DryCrud
5
5
  module Table
6
6
  # Test DryCrud::Table::Builder
7
7
  class BuilderTest < ActionView::TestCase
8
-
9
8
  # set dummy helper class for ActionView::TestCase
10
9
  self.helper_class = UtilityHelper
11
10
 
@@ -140,7 +139,6 @@ module DryCrud
140
139
 
141
140
  assert_dom_equal dom, table.to_html
142
141
  end
143
-
144
142
  end
145
143
  end
146
144
  end
@@ -117,7 +117,7 @@ class FormHelperTest < ActionView::TestCase
117
117
  .*?name="crud_test_model\[birthdate\]"/x, f)
118
118
  assert_match(/input .*?type="time"
119
119
  .*?name="crud_test_model\[gets_up_at\]"/x, f)
120
- assert_match(/input .*?type="datetime"
120
+ assert_match(/input .*?type="datetime-local"
121
121
  .*?name="crud_test_model\[last_seen\]"/x, f)
122
122
  assert_match(/input .*?type="checkbox"
123
123
  .*?name="crud_test_model\[human\]"/x, f)
@@ -130,4 +130,5 @@ class FormHelperTest < ActionView::TestCase
130
130
  def entry
131
131
  @entry ||= CrudTestModel.first
132
132
  end
133
+
133
134
  end
@@ -104,7 +104,7 @@ class FormatHelperTest < ActionView::TestCase
104
104
  m = crud_test_models(:AAAAA)
105
105
  assert_equal '1.100', format_type(m, :rating)
106
106
 
107
- m.rating = 3.145001 # you never know with these floats..
107
+ m.rating = 3.145001 # you never know with these floats..
108
108
  assert_equal '3.145', format_type(m, :rating)
109
109
  end
110
110
 
@@ -12,6 +12,8 @@ class TableHelperTest < ActionView::TestCase
12
12
  include CustomAssertions
13
13
  include CrudTestHelper
14
14
 
15
+ attr_accessor :params
16
+
15
17
  setup :reset_db, :setup_db, :create_test_data, :empty_params
16
18
  teardown :reset_db
17
19
 
@@ -26,9 +28,7 @@ class TableHelperTest < ActionView::TestCase
26
28
  end
27
29
 
28
30
  def empty_params
29
- def params
30
- {}
31
- end
31
+ @params = {}
32
32
  end
33
33
 
34
34
  test 'empty table renders message' do
@@ -47,8 +47,8 @@ class TableHelperTest < ActionView::TestCase
47
47
 
48
48
  test 'table with attrs' do
49
49
  expected = DryCrud::Table::Builder.table(
50
- %w(foo bar), self,
51
- class: 'table table-striped table-hover') do |t|
50
+ %w(foo bar), self,
51
+ class: 'table table-striped table-hover') do |t|
52
52
  t.attrs :size, :upcase
53
53
  end
54
54
  actual = plain_table(%w(foo bar), :size, :upcase)
@@ -110,10 +110,7 @@ class TableHelperTest < ActionView::TestCase
110
110
  end
111
111
 
112
112
  test 'standard list table with ascending sort params' do
113
- def params
114
- { sort: 'children', sort_dir: 'asc' }
115
- end
116
-
113
+ @params = { sort: 'children', sort_dir: 'asc' }
117
114
  @entries = CrudTestModel.all
118
115
 
119
116
  table = with_test_routing do
@@ -127,10 +124,7 @@ class TableHelperTest < ActionView::TestCase
127
124
  end
128
125
 
129
126
  test 'standard list table with descending sort params' do
130
- def params
131
- { sort: 'children', sort_dir: 'desc' }
132
- end
133
-
127
+ @params = { sort: 'children', sort_dir: 'desc' }
134
128
  @entries = CrudTestModel.all
135
129
 
136
130
  table = with_test_routing do
@@ -144,10 +138,7 @@ class TableHelperTest < ActionView::TestCase
144
138
  end
145
139
 
146
140
  test 'list table with custom column sort params' do
147
- def params
148
- { sort: 'chatty', sort_dir: 'asc' }
149
- end
150
-
141
+ @params = { sort: 'chatty', sort_dir: 'asc' }
151
142
  @entries = CrudTestModel.all
152
143
 
153
144
  table = with_test_routing do
@@ -33,10 +33,13 @@ class UtilityHelperTest < ActionView::TestCase
33
33
  end
34
34
 
35
35
  test 'default attributes do not include id and password' do
36
+ reset_db
37
+ setup_db
36
38
  assert_equal [:name, :email, :whatever, :children, :companion_id, :rating,
37
39
  :income, :birthdate, :gets_up_at, :last_seen, :human,
38
40
  :remarks, :created_at, :updated_at],
39
41
  default_crud_attrs
42
+ reset_db
40
43
  end
41
44
 
42
45
  test 'column types' do
@@ -7,14 +7,13 @@
7
7
  module CrudControllerTestHelper
8
8
 
9
9
  def test_index # :nodoc:
10
- get :index, test_params
10
+ get :index, params: test_params
11
11
  assert_response :success
12
- assert_template 'index'
13
12
  assert entries.present?
14
13
  end
15
14
 
16
15
  def test_index_json # :nodoc:
17
- get :index, test_params(format: 'json')
16
+ get :index, params: test_params(format: 'json')
18
17
  assert_response :success
19
18
  assert entries.present?
20
19
  assert @response.body.starts_with?('[{'), @response.body
@@ -23,9 +22,9 @@ module CrudControllerTestHelper
23
22
  def test_index_search # :nodoc:
24
23
  field = @controller.search_columns.first
25
24
  val = field && test_entry[field].to_s
26
- return if val.blank? # does not support search or no value in this field
25
+ return if val.blank? # does not support search or no value in this field
27
26
 
28
- get :index, test_params(q: val[0..((val.size + 1) / 2)])
27
+ get :index, params: test_params(q: val[0..((val.size + 1) / 2)])
29
28
  assert_response :success
30
29
  assert entries.present?
31
30
  assert entries.include?(test_entry)
@@ -33,31 +32,30 @@ module CrudControllerTestHelper
33
32
 
34
33
  def test_index_sort_asc # :nodoc:
35
34
  col = model_class.column_names.first
36
- get :index, test_params(sort: col, sort_dir: 'asc')
35
+ get :index, params: test_params(sort: col, sort_dir: 'asc')
37
36
  assert_response :success
38
37
  assert entries.present?
39
- sorted = entries.sort_by(&(col.to_sym))
38
+ sorted = entries.sort_by(&col.to_sym)
40
39
  assert_equal sorted, entries.to_a
41
40
  end
42
41
 
43
42
  def test_index_sort_desc # :nodoc:
44
43
  col = model_class.column_names.first
45
- get :index, test_params(sort: col, sort_dir: 'desc')
44
+ get :index, params: test_params(sort: col, sort_dir: 'desc')
46
45
  assert_response :success
47
46
  assert entries.present?
48
- sorted = entries.to_a.sort_by(&(col.to_sym))
47
+ sorted = entries.to_a.sort_by(&col.to_sym)
49
48
  assert_equal sorted.reverse, entries.to_a
50
49
  end
51
50
 
52
51
  def test_show # :nodoc:
53
- get :show, test_params(id: test_entry.id)
52
+ get :show, params: test_params(id: test_entry.id)
54
53
  assert_response :success
55
- assert_template 'show'
56
54
  assert_equal test_entry, entry
57
55
  end
58
56
 
59
57
  def test_show_json # :nodoc:
60
- get :show, test_params(id: test_entry.id, format: 'json')
58
+ get :show, params: test_params(id: test_entry.id, format: 'json')
61
59
  assert_response :success
62
60
  assert_equal test_entry, entry
63
61
  assert @response.body.starts_with?('{')
@@ -65,20 +63,19 @@ module CrudControllerTestHelper
65
63
 
66
64
  def test_show_with_non_existing_id_raises_record_not_found # :nodoc:
67
65
  assert_raise(ActiveRecord::RecordNotFound) do
68
- get :show, test_params(id: 9999)
66
+ get :show, params: test_params(id: 9999)
69
67
  end
70
68
  end
71
69
 
72
70
  def test_new # :nodoc:
73
- get :new, test_params
71
+ get :new, params: test_params
74
72
  assert_response :success
75
- assert_template 'new'
76
73
  assert entry.new_record?
77
74
  end
78
75
 
79
76
  def test_create # :nodoc:
80
77
  assert_difference("#{model_class.name}.count") do
81
- post :create, test_params(model_identifier => new_entry_attrs)
78
+ post :create, params: test_params(model_identifier => new_entry_attrs)
82
79
  end
83
80
  assert_redirected_to_show entry
84
81
  assert !entry.new_record?
@@ -87,24 +84,23 @@ module CrudControllerTestHelper
87
84
 
88
85
  def test_create_json # :nodoc:
89
86
  assert_difference("#{model_class.name}.count") do
90
- post :create, test_params(model_identifier => new_entry_attrs,
91
- format: 'json')
87
+ post :create, params: test_params(model_identifier => new_entry_attrs,
88
+ format: 'json')
92
89
  end
93
90
  assert_response :success
94
91
  assert @response.body.starts_with?('{"id":')
95
92
  end
96
93
 
97
94
  def test_edit # :nodoc:
98
- get :edit, test_params(id: test_entry.id)
95
+ get :edit, params: test_params(id: test_entry.id)
99
96
  assert_response :success
100
- assert_template 'edit'
101
97
  assert_equal test_entry, entry
102
98
  end
103
99
 
104
100
  def test_update # :nodoc:
105
101
  assert_no_difference("#{model_class.name}.count") do
106
- put :update, test_params(id: test_entry.id,
107
- model_identifier => edit_entry_attrs)
102
+ put :update, params: test_params(id: test_entry.id,
103
+ model_identifier => edit_entry_attrs)
108
104
  end
109
105
  assert_attrs_equal(edit_entry_attrs)
110
106
  assert_redirected_to_show entry
@@ -112,9 +108,9 @@ module CrudControllerTestHelper
112
108
 
113
109
  def test_update_json # :nodoc:
114
110
  assert_no_difference("#{model_class.name}.count") do
115
- put :update, test_params(id: test_entry.id,
116
- model_identifier => edit_entry_attrs,
117
- format: 'json')
111
+ put :update, params: test_params(id: test_entry.id,
112
+ model_identifier => edit_entry_attrs,
113
+ format: 'json')
118
114
  end
119
115
  assert_response :success
120
116
  assert @response.body.starts_with?('{"id":')
@@ -122,15 +118,15 @@ module CrudControllerTestHelper
122
118
 
123
119
  def test_destroy # :nodoc:
124
120
  assert_difference("#{model_class.name}.count", -1) do
125
- delete :destroy, test_params(id: test_entry.id)
121
+ delete :destroy, params: test_params(id: test_entry.id)
126
122
  end
127
123
  assert_redirected_to_index
128
124
  end
129
125
 
130
126
  def test_destroy_json # :nodoc:
131
127
  assert_difference("#{model_class.name}.count", -1) do
132
- delete :destroy, test_params(id: test_entry.id,
133
- format: 'json')
128
+ delete :destroy, params: test_params(id: test_entry.id,
129
+ format: 'json')
134
130
  end
135
131
  assert_response :success
136
132
  assert_equal '', @response.body.strip
@@ -140,6 +136,7 @@ module CrudControllerTestHelper
140
136
 
141
137
  def assert_redirected_to_index # :nodoc:
142
138
  assert_redirected_to test_params(action: 'index',
139
+ id: nil,
143
140
  returning: true)
144
141
  end
145
142
 
@@ -180,12 +177,12 @@ module CrudControllerTestHelper
180
177
 
181
178
  # Test object used in several tests.
182
179
  def test_entry
183
- fail 'Implement this method in your test class'
180
+ raise 'Implement this method in your test class'
184
181
  end
185
182
 
186
183
  # Attribute hash used in several tests.
187
184
  def test_entry_attrs
188
- fail 'Implement this method in your test class'
185
+ raise 'Implement this method in your test class'
189
186
  end
190
187
 
191
188
  # Attribute hash used in edit/update tests.
@@ -1,10 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
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:
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
8
 
9
9
  # A simple test helper to prepare the test database with a CrudTestModel model.
10
10
  # This helper is used to test the CrudController and various helpers
@@ -47,13 +47,11 @@ module CrudTestHelper
47
47
  # Look at the source to view the column definition.
48
48
  def setup_db
49
49
  without_transaction do
50
- silence_stream(STDOUT) do
51
- c = ActiveRecord::Base.connection
50
+ c = ActiveRecord::Base.connection
52
51
 
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
52
+ create_crud_test_models(c)
53
+ create_other_crud_test_models(c)
54
+ create_crud_test_models_other_crud_test_models(c)
57
55
 
58
56
  CrudTestModel.reset_column_information
59
57
  end
@@ -89,8 +87,8 @@ module CrudTestHelper
89
87
  def create_crud_test_models_other_crud_test_models(c)
90
88
  c.create_table :crud_test_models_other_crud_test_models,
91
89
  force: true do |t|
92
- t.belongs_to :crud_test_model
93
- t.belongs_to :other_crud_test_model
90
+ t.belongs_to :crud_test_model, index: { name: 'parent' }
91
+ t.belongs_to :other_crud_test_model, index: { name: 'other' }
94
92
  end
95
93
  end
96
94
 
@@ -100,7 +98,7 @@ module CrudTestHelper
100
98
  [:crud_test_models,
101
99
  :other_crud_test_models,
102
100
  :crud_test_models_other_crud_test_models].each do |table|
103
- c.drop_table(table) if c.table_exists?(table)
101
+ c.drop_table(table) if c.data_source_exists?(table)
104
102
  end
105
103
  end
106
104
 
@@ -3,7 +3,7 @@
3
3
  # A dummy model used for general testing.
4
4
  class CrudTestModel < ActiveRecord::Base #:nodoc:
5
5
 
6
- belongs_to :companion, class_name: 'CrudTestModel'
6
+ belongs_to :companion, class_name: 'CrudTestModel', optional: true
7
7
  has_and_belongs_to_many :others, class_name: 'OtherCrudTestModel'
8
8
  has_many :mores, class_name: 'OtherCrudTestModel',
9
9
  foreign_key: :more_id
@@ -26,7 +26,7 @@ class CrudTestModel < ActiveRecord::Base #:nodoc:
26
26
  def protect_if_companion
27
27
  if companion.present?
28
28
  errors.add(:base, 'Cannot destroy model with companion')
29
- false
29
+ throw :abort
30
30
  end
31
31
  end
32
32
 
@@ -36,9 +36,13 @@ end
36
36
  class OtherCrudTestModel < ActiveRecord::Base #:nodoc:
37
37
 
38
38
  has_and_belongs_to_many :others, class_name: 'CrudTestModel'
39
- belongs_to :more, foreign_key: :more_id, class_name: 'CrudTestModel'
39
+ belongs_to :more,
40
+ foreign_key: :more_id,
41
+ class_name: 'CrudTestModel',
42
+ optional: true
40
43
 
41
44
  def to_s
42
45
  name
43
46
  end
47
+
44
48
  end
@@ -2,7 +2,8 @@
2
2
 
3
3
  # Controller for the dummy model.
4
4
  class CrudTestModelsController < CrudController #:nodoc:
5
- HANDLE_PREFIX = 'handle_'
5
+
6
+ HANDLE_PREFIX = 'handle_'.freeze
6
7
 
7
8
  self.search_columns = [:name, :whatever, :remarks]
8
9
  self.sort_mappings = { chatty: 'length(remarks)' }
@@ -21,19 +22,17 @@ class CrudTestModelsController < CrudController #:nodoc:
21
22
  attr_reader :called_callbacks
22
23
  attr_accessor :should_redirect
23
24
 
24
- hide_action :called_callbacks, :should_redirect, :should_redirect=
25
-
26
25
  # don't use the standard layout as it may require different routes
27
26
  # than just the test route for this controller
28
27
  layout false
29
28
 
30
29
  def index
31
30
  entries
32
- render text: 'index js' if request.format.js?
31
+ render plain: 'index js' if request.format.js?
33
32
  end
34
33
 
35
34
  def show
36
- render text: 'custom html' if entry.name == 'BBBBB'
35
+ render html: 'custom html' if entry.name == 'BBBBB'
37
36
  end
38
37
 
39
38
  def create
@@ -54,8 +53,6 @@ class CrudTestModelsController < CrudController #:nodoc:
54
53
  entries
55
54
  end
56
55
 
57
- private
58
-
59
56
  def build_entry
60
57
  entry = super
61
58
  if params[model_identifier]
@@ -68,14 +65,13 @@ class CrudTestModelsController < CrudController #:nodoc:
68
65
  def handle_name
69
66
  if entry.name == 'illegal'
70
67
  flash[:alert] = 'illegal name'
71
- false
68
+ throw :abort
72
69
  end
73
70
  end
74
71
 
75
72
  # callback to redirect if @should_redirect is set
76
73
  def possibly_redirect
77
74
  redirect_to action: 'index' if should_redirect && !performed?
78
- !should_redirect
79
75
  end
80
76
 
81
77
  def set_companions