rows_controller 0.4.4 → 1.0.1
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.
- checksums.yaml +7 -0
- data/.watchr +39 -43
- data/Gemfile +5 -2
- data/Gemfile.lock +72 -77
- data/MIT-LICENSE +1 -1
- data/README.md +20 -4
- data/app/controllers/rows_controller.rb +68 -53
- data/app/controllers/rows_controller/helpers.rb +68 -40
- data/app/controllers/rows_ext_controller.rb +18 -0
- data/app/views/rows/_form.slim +2 -0
- data/app/views/rows/_list.slim +18 -0
- data/app/views/rows/_list_footer.slim +5 -0
- data/app/views/rows/_list_header.slim +7 -0
- data/app/views/rows/_list_row.slim +10 -0
- data/app/views/rows/_row_buttons.slim +14 -0
- data/app/views/rows/_submit.slim +19 -0
- data/app/views/rows/_submit_part.slim +4 -0
- data/app/views/rows/edit.slim +17 -0
- data/app/views/rows/index.slim +10 -0
- data/app/views/rows/new.slim +16 -0
- data/app/views/rows/show.slim +17 -0
- data/app/views/shared/_error_explanation.slim +10 -0
- data/app/views/shared/_show_flash.slim +9 -0
- data/lib/rows_controller.rb +1 -0
- data/lib/rows_controller/locales/de.yml +23 -0
- data/lib/rows_controller/locales/en.yml +16 -27
- data/lib/rows_controller/version.rb +1 -1
- data/spec/controllers/orders_spec.rb +28 -3
- data/spec/controllers/rows_ext_spec.rb +6 -0
- data/spec/controllers/rows_spec.rb +5 -0
- data/spec/dummy/app/controllers/orders_controller.rb +11 -2
- data/spec/dummy/app/models/order.rb +5 -4
- data/spec/dummy/app/views/layouts/application.html.erb +1 -1
- data/spec/dummy/db/schema.rb +2 -2
- data/spec/{integration → features}/order_spec.rb +12 -3
- data/spec/spec_helper.rb +19 -52
- metadata +31 -34
- data/.rvmrc +0 -4
- data/app/views/rows/_form.html.erb +0 -3
- data/app/views/rows/_list.html.erb +0 -30
- data/app/views/rows/_submit.html.erb +0 -22
- data/app/views/rows/_submit_part.html.erb +0 -4
- data/app/views/rows/edit.html.erb +0 -22
- data/app/views/rows/index.html.erb +0 -17
- data/app/views/rows/new.html.erb +0 -22
- data/app/views/rows/show.html.erb +0 -24
- data/app/views/shared/_error_explanation.html.erb +0 -14
- data/app/views/shared/_flash.html.erb +0 -9
- data/spec/support/describe_private.rb +0 -14
@@ -1,48 +1,81 @@
|
|
1
1
|
class RowsController < ApplicationController
|
2
|
+
helper_method :set_resource, :set_resources
|
3
|
+
helper_method :resource, :resources, :resource_columns, :resource_format
|
4
|
+
helper_method :model_class, :model_name, :model_symbol, :model_symbol_plural
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
# resources/@rows
|
7
|
+
def set_resources(rows = nil)
|
8
|
+
rows ||= model_class.all
|
9
|
+
instance_variable_set("@#{model_symbol_plural}", rows)
|
10
|
+
@rows = rows
|
6
11
|
end
|
7
12
|
|
8
|
-
|
9
|
-
|
10
|
-
|
13
|
+
def set_resource(row = nil)
|
14
|
+
row ||= model_class.find(params[:id].to_i)
|
15
|
+
instance_variable_set("@#{model_symbol}", row)
|
16
|
+
@row = row
|
17
|
+
end
|
11
18
|
|
12
|
-
|
13
|
-
|
14
|
-
|
19
|
+
def resources
|
20
|
+
@rows || set_resources
|
21
|
+
end
|
15
22
|
|
16
23
|
def resource
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
@row || set_resource
|
25
|
+
end
|
26
|
+
|
27
|
+
def resource_columns
|
28
|
+
return model_class.column_headers if model_class.respond_to?(:column_headers)
|
29
|
+
return ['to_s'] unless model_class.respond_to?(:content_columns)
|
30
|
+
['id'] + model_class.content_columns.collect{|c| c.name }
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def resource_whitelist
|
35
|
+
raise "RowsController requires private method 'resource_whitelist' in controller <#{params[:controller]}>"
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
public
|
40
|
+
# low level resource methods
|
41
|
+
# can be monkey patched
|
42
|
+
def resource_new
|
43
|
+
if params[model_symbol]
|
44
|
+
set_resource model_class.new(resource_params)
|
45
|
+
else
|
46
|
+
set_resource model_class.new
|
28
47
|
end
|
29
48
|
end
|
30
49
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
value
|
50
|
+
def resource_create
|
51
|
+
resource_new
|
52
|
+
resource.save
|
35
53
|
end
|
36
54
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
55
|
+
def resource_update
|
56
|
+
unless RAILS4
|
57
|
+
resource.update_attributes(resource_params)
|
58
|
+
else
|
59
|
+
resource.update(resource_params)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def resource_destroy
|
64
|
+
resource.destroy
|
40
65
|
end
|
41
66
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
67
|
+
|
68
|
+
# redirections
|
69
|
+
# can be monkey patched
|
70
|
+
def redirect_to_index
|
71
|
+
redirect_to action: 'index'
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
# dynamic model related methods
|
76
|
+
def self.model_class(model_class = nil)
|
77
|
+
@model_class = model_class unless model_class.nil?
|
78
|
+
@model_class
|
46
79
|
end
|
47
80
|
|
48
81
|
def model_class
|
@@ -62,24 +95,19 @@ class RowsController < ApplicationController
|
|
62
95
|
@_model_symbol_plural ||= model_name.pluralize.underscore
|
63
96
|
end
|
64
97
|
|
65
|
-
|
66
|
-
return model_class.column_headers if model_class.respond_to?(:column_headers)
|
67
|
-
return ['to_s'] unless model_class.respond_to?(:content_columns)
|
68
|
-
['id'] + model_class.content_columns.collect{|c| c.name }
|
69
|
-
end
|
70
|
-
|
98
|
+
# formatting
|
71
99
|
def resource_format(x)
|
72
|
-
|
100
|
+
return '--'.html_safe if x.nil?
|
73
101
|
bool = x.class == Time || x.class == Date || x.class == DateTime ||
|
74
102
|
x.class == ActiveSupport::TimeWithZone
|
75
|
-
return x.strftime(
|
103
|
+
return x.strftime('%d.%m.%Y').html_safe if bool
|
104
|
+
# return I18n.l(x) if bool
|
76
105
|
return x.to_s.html_safe if x.class == Fixnum
|
77
106
|
return 'X'.html_safe if x.class == TrueClass
|
78
107
|
return ' '.html_safe if x.class == FalseClass
|
79
108
|
return x.call if x.class == Proc
|
80
109
|
return '---'.html_safe if x.empty?
|
81
110
|
str = x.to_s
|
82
|
-
## str = UTF8FY.iconv(x.to_s) if APPLICATION_OPTIONS[:customization] == :dk
|
83
111
|
return str.gsub(/\r*\n/, '<br/>')
|
84
112
|
end
|
85
113
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class RowsExtController < RowsController
|
2
|
+
|
3
|
+
def copy
|
4
|
+
set_resource resource.dup
|
5
|
+
resource.id = nil
|
6
|
+
respond_to do |format|
|
7
|
+
format.html { render :action => :new }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def multi_deletion
|
12
|
+
items = params[:multi_tick] || []
|
13
|
+
items -= ['']
|
14
|
+
items.map { |id| model_class.find_by_id(id.to_i).destroy }
|
15
|
+
redirect_to :action => :index
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
ruby:
|
2
|
+
# Requires resources columns
|
3
|
+
# Column :multi_selection generates a check_box
|
4
|
+
|
5
|
+
cnt = 0
|
6
|
+
|
7
|
+
table.rows-list
|
8
|
+
thead
|
9
|
+
tr
|
10
|
+
= render 'list_header', columns: columns
|
11
|
+
|
12
|
+
tbody
|
13
|
+
- resources.each do |resource|
|
14
|
+
- cnt += 1
|
15
|
+
tr id="row_#{resource.id}" class="#{%w{odd even}.at(cnt % 2)}"
|
16
|
+
= render 'list_row', resource: resource, columns: columns
|
17
|
+
td
|
18
|
+
= render 'row_buttons', row: resource
|
@@ -0,0 +1,14 @@
|
|
1
|
+
ruby:
|
2
|
+
# Fills the last column of the list with "buttons".
|
3
|
+
# Requires row current_controller (supplied by Rails)
|
4
|
+
if defined?(current_controller)
|
5
|
+
url = "/#{current_controller}/#{row.id}"
|
6
|
+
else
|
7
|
+
url = url_for row
|
8
|
+
end
|
9
|
+
|
10
|
+
= link_to t('button.show').html_safe, url
|
11
|
+
= link_to t('button.edit').html_safe, url + '/edit'
|
12
|
+
= link_to t('button.delete').html_safe, url,
|
13
|
+
remote: true,
|
14
|
+
data: { confirm: 'Are you sure?' }, method: :delete
|
@@ -0,0 +1,19 @@
|
|
1
|
+
ruby:
|
2
|
+
# submit links and/or buttons
|
3
|
+
# Requires
|
4
|
+
# content_for(:submit_<left|right>)
|
5
|
+
# left|right (links names)
|
6
|
+
|
7
|
+
# left|right must be used inside a form.
|
8
|
+
# The clean content_for variant may be used unbounded.
|
9
|
+
|
10
|
+
left ||= nil
|
11
|
+
right ||= nil
|
12
|
+
|
13
|
+
div.rows-submit style="width:100%"
|
14
|
+
input style="display:none" type="submit" name="commit" value="OK"
|
15
|
+
p style="float:left"
|
16
|
+
= render '/rows/submit_part', content: :submit_left, names: left
|
17
|
+
p style="float:right"
|
18
|
+
= render '/rows/submit_part', content: :submit_right, names: right
|
19
|
+
div style="clear:both"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
ruby:
|
2
|
+
# Template for RowsController (edit)
|
3
|
+
# Requires resource model_name model_symbol
|
4
|
+
# partial 'form' template
|
5
|
+
|
6
|
+
content_for(:submit_left) {
|
7
|
+
link_to t('button.back').html_safe, "/#{controller_name}", class: 'button' }
|
8
|
+
|
9
|
+
fieldset class="wide mask" id=model_symbol
|
10
|
+
legend = t('ui.editing', model: model_name)
|
11
|
+
= render '/shared/error_explanation'
|
12
|
+
|
13
|
+
- @multipart ||= false
|
14
|
+
/"/#{controller_name}/update/#{@row.id}"
|
15
|
+
= form_for resource, url: "/#{controller_name}/#{@row.id}", html: {multipart: @multipart} do |f|
|
16
|
+
= render 'form', f: f
|
17
|
+
= render '/rows/submit', right: ['update', 'OK']
|
@@ -0,0 +1,10 @@
|
|
1
|
+
- # Template for RowsController (index)
|
2
|
+
- # Requires resources model_name resource_columns
|
3
|
+
|
4
|
+
- if resources.length > 0
|
5
|
+
h1 = t('ui.listing', model: model_name)
|
6
|
+
= render '/rows/list', resources: resources, columns: resource_columns
|
7
|
+
- else
|
8
|
+
h1 = t('ui.empty_list', model: model_name)
|
9
|
+
|
10
|
+
= render 'list_footer'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
ruby:
|
2
|
+
# Template for RowsController (new)
|
3
|
+
# Requires resource model_name model_symbol
|
4
|
+
# partial 'form' template
|
5
|
+
|
6
|
+
content_for(:submit_left) {
|
7
|
+
link_to t('button.back').html_safe, "/#{controller_name}", class: 'button' }
|
8
|
+
|
9
|
+
fieldset.wide class="mask" id=model_symbol
|
10
|
+
legend = t('ui.new', model: model_name)
|
11
|
+
= render '/shared/error_explanation'
|
12
|
+
|
13
|
+
- @multipart ||= false
|
14
|
+
= form_for resource, html: {multipart: @multipart} do |f|
|
15
|
+
= render 'form', f: f
|
16
|
+
= render '/rows/submit', right: ['create', 'OK']
|
@@ -0,0 +1,17 @@
|
|
1
|
+
ruby:
|
2
|
+
# Template for RowsController (show)
|
3
|
+
# Requires resource model_name model_symbol
|
4
|
+
# partial 'form' template
|
5
|
+
|
6
|
+
content_for(:submit_left) {
|
7
|
+
link_to t('button.back').html_safe, url_for(action: :index), class: 'button' }
|
8
|
+
content_for(:submit_right) {
|
9
|
+
link_to t('button.edit').html_safe, url_for(action: :edit), class: 'button' }
|
10
|
+
|
11
|
+
fieldset.show class="wide mask" id=model_symbol
|
12
|
+
legend = t('ui.showing', model: model_name)
|
13
|
+
= render '/shared/error_explanation'
|
14
|
+
|
15
|
+
= form_for resource do |f|
|
16
|
+
= render 'form', f: f
|
17
|
+
= render '/rows/submit'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
/ # Generates error messages
|
2
|
+
/ # requires resource
|
3
|
+
|
4
|
+
- if resource && resource.respond_to?(:errors) && resource.errors.any?
|
5
|
+
div#error_explanation
|
6
|
+
h2 #{pluralize(resource.errors.count, "error")} prohibited this template from being saved:
|
7
|
+
|
8
|
+
ul
|
9
|
+
- resource.errors.full_messages.each do |msg|
|
10
|
+
li = msg
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/ # Show complete flash
|
2
|
+
/ # flash[:notice] are faded out after 5 seconds
|
3
|
+
/ # never call this file just 'flash' ==> empties flash
|
4
|
+
|
5
|
+
- (flash || {}).each do |key, value|
|
6
|
+
p class="flash_#{key}" = sanitize(value)
|
7
|
+
- if key == :notice
|
8
|
+
javascript:
|
9
|
+
$('.flash_notice').fadeOut(5000, function() { $(this).remove(); });
|
data/lib/rows_controller.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
# under construction; see english version
|
2
|
+
de:
|
3
|
+
|
4
|
+
ui:
|
5
|
+
created: '%{model} erstellt.'
|
6
|
+
destroyed: '%{model} gelöscht.'
|
7
|
+
updated: '%{model} geändert.'
|
8
|
+
|
9
|
+
editing: 'Editiere %{model}'
|
10
|
+
listing: 'Liste %{model}'
|
11
|
+
new: 'Neu %{model}'
|
12
|
+
showing: 'Zeige %{model}'
|
13
|
+
|
14
|
+
empty_list: 'Liste %{model} is leer.'
|
15
|
+
|
16
|
+
button:
|
17
|
+
back: "Zurück"
|
18
|
+
create: 'Neu'
|
19
|
+
update: 'Speichern'
|
20
|
+
delete: 'Lösche'
|
21
|
+
edit: 'Edititeren'
|
22
|
+
show: 'Zeige'
|
23
|
+
clone: 'Klonen'
|
@@ -1,33 +1,22 @@
|
|
1
1
|
en:
|
2
|
-
flash:
|
3
|
-
actions:
|
4
|
-
create:
|
5
|
-
notice: "%{resource_name} was successfully created."
|
6
|
-
alert: ""
|
7
|
-
update:
|
8
|
-
notice: "%{resource_name} was successfully updated."
|
9
|
-
alert: ""
|
10
|
-
destroy:
|
11
|
-
notice: "%{resource_name} was successfully destroyed."
|
12
|
-
alert: "%{resource_name} could not be destroyed."
|
13
2
|
|
14
3
|
ui:
|
15
|
-
created:
|
16
|
-
|
17
|
-
|
18
|
-
listing: 'Listing %{model}'
|
19
|
-
new: 'New %{model}'
|
20
|
-
show: 'Show %{model}'
|
21
|
-
updated: '%{model} updated.'
|
4
|
+
created: '%{model} created.'
|
5
|
+
destroyed: '%{model} deleted.'
|
6
|
+
updated: '%{model} updated.'
|
22
7
|
|
23
|
-
|
24
|
-
|
8
|
+
editing: 'Editing %{model}'
|
9
|
+
listing: 'Listing %{model}'
|
10
|
+
new: 'New %{model}'
|
11
|
+
showing: 'Showing %{model}'
|
25
12
|
|
26
|
-
|
27
|
-
back: 'Back'
|
28
|
-
edit: 'Edit'
|
29
|
-
delete: 'Delete'
|
30
|
-
show: 'Show'
|
13
|
+
empty_list: 'List %{model} is empty'
|
31
14
|
|
32
|
-
|
33
|
-
|
15
|
+
button:
|
16
|
+
back: 'Back'
|
17
|
+
create: 'Create'
|
18
|
+
update: 'Update'
|
19
|
+
delete: 'Delete'
|
20
|
+
edit: 'Edit'
|
21
|
+
show: 'Show'
|
22
|
+
clone: 'Clone'
|
@@ -31,6 +31,7 @@ describe OrdersController, ':' do
|
|
31
31
|
get :show, :id => order.id
|
32
32
|
subject.send(:resource).should == order
|
33
33
|
assigns(:order).should == order
|
34
|
+
assigns(:row).should == order
|
34
35
|
end
|
35
36
|
|
36
37
|
it 'checking resources' do
|
@@ -38,6 +39,7 @@ describe OrdersController, ':' do
|
|
38
39
|
subject.send(:resources).should be_a_kind_of(Array)
|
39
40
|
assigns(:orders).should be_a_kind_of(Array)
|
40
41
|
assigns(:orders).should == Order.all
|
42
|
+
assigns(:rows).should == Order.all
|
41
43
|
end
|
42
44
|
|
43
45
|
it 'checking model_class' do
|
@@ -47,6 +49,31 @@ describe OrdersController, ':' do
|
|
47
49
|
subject.send(:model_symbol).should == 'order'
|
48
50
|
subject.send(:model_symbol_plural).should == 'orders'
|
49
51
|
end
|
52
|
+
|
53
|
+
it 'should update' do
|
54
|
+
put :update, { id: order.id }
|
55
|
+
response.should be_true
|
56
|
+
response.should redirect_to(action: :edit)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should update #2' do
|
60
|
+
put :update, { id: order.id, commit: 'OK' }
|
61
|
+
response.should be_true
|
62
|
+
response.should redirect_to(action: :index)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should not update' do
|
66
|
+
put :update, { id: order.id, order: {name: 'error'} }
|
67
|
+
response.should be_success
|
68
|
+
response.should render_template('rows/edit')
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should not create' do
|
72
|
+
post :create, { id: order.id, order: {name: 'error'} }
|
73
|
+
response.should be_success
|
74
|
+
response.should render_template('rows/new')
|
75
|
+
end
|
76
|
+
|
50
77
|
end
|
51
78
|
|
52
79
|
|
@@ -57,9 +84,7 @@ end
|
|
57
84
|
describe CategoriesController do
|
58
85
|
it 'checking model_class' do
|
59
86
|
get :index
|
60
|
-
|
61
|
-
# subject.send(:model_class).should be_a(Order)
|
62
|
-
# subject.send(:model_class).should == Order
|
87
|
+
subject.send(:model_class).should == Order
|
63
88
|
subject.send(:model_name).should == 'Order'
|
64
89
|
subject.send(:model_symbol).should == 'order'
|
65
90
|
subject.send(:model_symbol_plural).should == 'orders'
|