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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.watchr +39 -43
  3. data/Gemfile +5 -2
  4. data/Gemfile.lock +72 -77
  5. data/MIT-LICENSE +1 -1
  6. data/README.md +20 -4
  7. data/app/controllers/rows_controller.rb +68 -53
  8. data/app/controllers/rows_controller/helpers.rb +68 -40
  9. data/app/controllers/rows_ext_controller.rb +18 -0
  10. data/app/views/rows/_form.slim +2 -0
  11. data/app/views/rows/_list.slim +18 -0
  12. data/app/views/rows/_list_footer.slim +5 -0
  13. data/app/views/rows/_list_header.slim +7 -0
  14. data/app/views/rows/_list_row.slim +10 -0
  15. data/app/views/rows/_row_buttons.slim +14 -0
  16. data/app/views/rows/_submit.slim +19 -0
  17. data/app/views/rows/_submit_part.slim +4 -0
  18. data/app/views/rows/edit.slim +17 -0
  19. data/app/views/rows/index.slim +10 -0
  20. data/app/views/rows/new.slim +16 -0
  21. data/app/views/rows/show.slim +17 -0
  22. data/app/views/shared/_error_explanation.slim +10 -0
  23. data/app/views/shared/_show_flash.slim +9 -0
  24. data/lib/rows_controller.rb +1 -0
  25. data/lib/rows_controller/locales/de.yml +23 -0
  26. data/lib/rows_controller/locales/en.yml +16 -27
  27. data/lib/rows_controller/version.rb +1 -1
  28. data/spec/controllers/orders_spec.rb +28 -3
  29. data/spec/controllers/rows_ext_spec.rb +6 -0
  30. data/spec/controllers/rows_spec.rb +5 -0
  31. data/spec/dummy/app/controllers/orders_controller.rb +11 -2
  32. data/spec/dummy/app/models/order.rb +5 -4
  33. data/spec/dummy/app/views/layouts/application.html.erb +1 -1
  34. data/spec/dummy/db/schema.rb +2 -2
  35. data/spec/{integration → features}/order_spec.rb +12 -3
  36. data/spec/spec_helper.rb +19 -52
  37. metadata +31 -34
  38. data/.rvmrc +0 -4
  39. data/app/views/rows/_form.html.erb +0 -3
  40. data/app/views/rows/_list.html.erb +0 -30
  41. data/app/views/rows/_submit.html.erb +0 -22
  42. data/app/views/rows/_submit_part.html.erb +0 -4
  43. data/app/views/rows/edit.html.erb +0 -22
  44. data/app/views/rows/index.html.erb +0 -17
  45. data/app/views/rows/new.html.erb +0 -22
  46. data/app/views/rows/show.html.erb +0 -24
  47. data/app/views/shared/_error_explanation.html.erb +0 -14
  48. data/app/views/shared/_flash.html.erb +0 -9
  49. 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
- def self.model_class(model_class = nil)
4
- @model_class = model_class unless model_class.nil?
5
- @model_class
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
- protected
10
- DATE_FORMAT = '%d.%m.%Y'
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
- helper_method :resource, :resources, :columns,
13
- :model_class, :model_name, :model_symbol, :model_symbol_plural,
14
- :resource_format
19
+ def resources
20
+ @rows || set_resources
21
+ end
15
22
 
16
23
  def resource
17
- return @_resource if @_resource
18
-
19
- self.resource = begin
20
- name = model_symbol
21
- if id = params["#{name}_id"] || params[:id]
22
- model_class.find_by_id(id).tap do |r|
23
- r.attributes = params[name] unless request.get?
24
- end
25
- else
26
- model_class.new(params[name])
27
- end
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 resource=(value)
32
- @_resource = value
33
- instance_variable_set("@#{model_symbol}", value)
34
- value
50
+ def resource_create
51
+ resource_new
52
+ resource.save
35
53
  end
36
54
 
37
- def resources
38
- return @_resources if @_resources
39
- self.resources = model_class.all
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
- def resources=(value)
43
- @_resources = value
44
- instance_variable_set("@#{model_symbol_plural}", value)
45
- value
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
- def columns
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
- x = ''.html_safe if x.nil?
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(DATE_FORMAT).html_safe if bool
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 '&ensp;'.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,2 @@
1
+ div style="color:red"
2
+ | Rendering requires partial '#{model_symbol}/_form.html.erb'
@@ -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,5 @@
1
+ / Default list footer presenting a 'Create' button linked to action new.
2
+ / May be overwriten for e.g. pagination
3
+
4
+ div.list-footer
5
+ = link_to t('button.create').html_safe, url_for(action: :new), class: 'button'
@@ -0,0 +1,7 @@
1
+ / Default list header.
2
+ / Requires columns.
3
+ / May be overwriten for e.g. pagination and/or sorting.
4
+
5
+ - columns.each do |column|
6
+ th = column == :multi_selection ? '' : t("header.#{column}")
7
+
@@ -0,0 +1,10 @@
1
+ ruby:
2
+ # Requires resource columns
3
+
4
+ - columns.each do |column|
5
+ td
6
+ - case column
7
+ - when :multi_selection
8
+ = check_box_tag('multi_selection[]', resource.id)
9
+ - else
10
+ = resource_format(resource.send(column))
@@ -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,4 @@
1
+ = content_for(content)
2
+ - (names || []).each do |name|
3
+ button.button type="submit" name="commit" value=name
4
+ = t(name, scope: :button)
@@ -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(); });
@@ -2,4 +2,5 @@ require 'rows_controller/engine.rb'
2
2
 
3
3
  require 'active_support/i18n'
4
4
  I18n.load_path << File.expand_path('../rows_controller/locales/en.yml', __FILE__)
5
+ I18n.load_path << File.expand_path('../rows_controller/locales/de.yml', __FILE__)
5
6
  I18n.reload!
@@ -0,0 +1,23 @@
1
+ # under construction; see english version
2
+ de:
3
+
4
+ ui:
5
+ created: '%{model} erstellt.'
6
+ destroyed: '%{model} gel&ouml;scht.'
7
+ updated: '%{model} ge&auml;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&uuml;ck"
18
+ create: 'Neu'
19
+ update: 'Speichern'
20
+ delete: 'L&ouml;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: '%{model} created.'
16
- deleted: '%{model} deleted.'
17
- empty_list: 'List %{model} is empty'
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
- create: 'Create'
24
- update: 'Update'
8
+ editing: 'Editing %{model}'
9
+ listing: 'Listing %{model}'
10
+ new: 'New %{model}'
11
+ showing: 'Showing %{model}'
25
12
 
26
- button:
27
- back: 'Back'
28
- edit: 'Edit'
29
- delete: 'Delete'
30
- show: 'Show'
13
+ empty_list: 'List %{model} is empty'
31
14
 
32
- header:
33
- # to_s: 'Test'
15
+ button:
16
+ back: 'Back'
17
+ create: 'Create'
18
+ update: 'Update'
19
+ delete: 'Delete'
20
+ edit: 'Edit'
21
+ show: 'Show'
22
+ clone: 'Clone'
@@ -1,3 +1,3 @@
1
1
  module Rows
2
- VERSION = '0.4.4'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -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
- # both not working, Why?
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'