rows_controller 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.rspec +2 -0
  2. data/.rvmrc +3 -0
  3. data/.watchr +56 -0
  4. data/Gemfile +16 -0
  5. data/Gemfile.lock +136 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +91 -0
  8. data/Rakefile +30 -0
  9. data/app/controllers/rows_controller.rb +80 -0
  10. data/app/controllers/rows_controller/helpers.rb +86 -0
  11. data/app/views/rows/_form.html.erb +3 -0
  12. data/app/views/rows/_list.html.erb +31 -0
  13. data/app/views/rows/_submit.html.erb +22 -0
  14. data/app/views/rows/_submit_part.html.erb +4 -0
  15. data/app/views/rows/destroy.js.erb +1 -0
  16. data/app/views/rows/edit.html.erb +22 -0
  17. data/app/views/rows/index.html.erb +20 -0
  18. data/app/views/rows/new.html.erb +22 -0
  19. data/app/views/rows/show.html.erb +25 -0
  20. data/app/views/shared/_error_explanation.html.erb +14 -0
  21. data/app/views/shared/_flash.html.erb +9 -0
  22. data/lib/rows_controller.rb +5 -0
  23. data/lib/rows_controller/engine.rb +4 -0
  24. data/lib/rows_controller/locales/en.yml +33 -0
  25. data/lib/rows_controller/version.rb +3 -0
  26. data/rows_controller.gemspec +27 -0
  27. data/spec/controllers/orders_spec.rb +65 -0
  28. data/spec/controllers/rows_spec.rb +19 -0
  29. data/spec/dummy/Rakefile +7 -0
  30. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  31. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  32. data/spec/dummy/app/controllers/orders_controller.rb +3 -0
  33. data/spec/dummy/app/models/order.rb +10 -0
  34. data/spec/dummy/app/views/layouts/application.html.erb +13 -0
  35. data/spec/dummy/app/views/orders/_form.html.erb +12 -0
  36. data/spec/dummy/config.ru +4 -0
  37. data/spec/dummy/config/application.rb +49 -0
  38. data/spec/dummy/config/boot.rb +9 -0
  39. data/spec/dummy/config/database.yml +22 -0
  40. data/spec/dummy/config/environment.rb +5 -0
  41. data/spec/dummy/config/environments/development.rb +30 -0
  42. data/spec/dummy/config/environments/production.rb +60 -0
  43. data/spec/dummy/config/environments/test.rb +40 -0
  44. data/spec/dummy/config/initializers/secret_token.rb +1 -0
  45. data/spec/dummy/config/initializers/session_store.rb +8 -0
  46. data/spec/dummy/config/routes.rb +7 -0
  47. data/spec/dummy/db/migrate/001_create_orders.rb +10 -0
  48. data/spec/dummy/db/schema.rb +23 -0
  49. data/spec/dummy/db/seeds.rb +7 -0
  50. data/spec/dummy/public/favicon.ico +0 -0
  51. data/spec/dummy/script/rails +6 -0
  52. data/spec/integration/order_spec.rb +45 -0
  53. data/spec/models/order_spec.rb +7 -0
  54. data/spec/spec_helper.rb +63 -0
  55. data/spec/support/describe_private.rb +14 -0
  56. metadata +164 -0
@@ -0,0 +1,86 @@
1
+ class RowsController < ApplicationController
2
+
3
+ def self.model_class(model_class = nil)
4
+ @model_class = model_class unless model_class.nil?
5
+ @model_class
6
+ end
7
+
8
+
9
+ protected
10
+ DATE_FORMAT = '%d.%m.%Y'
11
+
12
+ helper_method :resource, :resources, :columns,
13
+ :model_class, :model_name, :model_symbol, :model_symbol_plural,
14
+ :resources_format
15
+
16
+ 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
28
+ end
29
+ end
30
+
31
+ def resource=(value)
32
+ @_resource = value
33
+ instance_variable_set("@#{model_symbol}", value)
34
+ value
35
+ end
36
+
37
+ def resources
38
+ return @_resources if @_resources
39
+ self.resources = model_class.all
40
+ end
41
+
42
+ def resources=(value)
43
+ @_resources = value
44
+ instance_variable_set("@#{model_symbol_plural}", value)
45
+ value
46
+ end
47
+
48
+ def model_class
49
+ @model_class ||= self.class.model_class ||
50
+ params[:controller].singularize.camelize.constantize
51
+ end
52
+
53
+ def model_name
54
+ @_model_name ||= model_class.name
55
+ end
56
+
57
+ def model_symbol
58
+ @_model_symbol ||= model_name.underscore
59
+ end
60
+
61
+ def model_symbol_plural
62
+ @_model_symbol_plural ||= model_name.pluralize.underscore
63
+ end
64
+
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
+
71
+ def resources_format(x)
72
+ x = '' if x.nil?
73
+ bool = x.class == Time || x.class == Date || x.class == DateTime ||
74
+ x.class == ActiveSupport::TimeWithZone
75
+ return x.strftime(DATE_FORMAT) if bool
76
+ return x.to_s if x.class == Fixnum
77
+ return 'X' if x.class == TrueClass
78
+ return '&ensp;' if x.class == FalseClass
79
+ return x.call if x.class == Proc
80
+ return '---' if x.empty?
81
+ str = x.to_s
82
+ ## str = UTF8FY.iconv(x.to_s) if APPLICATION_OPTIONS[:customization] == :dk
83
+ return str.gsub(/\r*\n/, '<br/>')
84
+ end
85
+
86
+ end
@@ -0,0 +1,3 @@
1
+ <div style="color:red">
2
+ Rendering requires partial '<%= model_symbol %>/_form.html.erb'
3
+ </div>
@@ -0,0 +1,31 @@
1
+ <%
2
+ # Requires resources model_class columns
3
+ # /home/dk/docu/app/views/restful/_list.html.erb
4
+
5
+ cnt = 0
6
+ %>
7
+
8
+ <table class="rows-list">
9
+ <tr>
10
+ <% columns.each do |column| %>
11
+ <th> <%= t("header.#{column}", :default => column) %> </th>
12
+ <% end %>
13
+ </tr>
14
+ <% resources.each do |resource|
15
+ url = "/#{controller_name}/#{resource.id}"
16
+ cnt += 1
17
+ %>
18
+ <tr id="row_<%= resource.id%>" class="<%= %w{odd even}.at(cnt % 2) %>" >
19
+ <% columns.each do |column| %>
20
+ <td> <%= resources_format(resource.send(column)) %> </td>
21
+ <% end %>
22
+ <td>
23
+ <%= link_to t('button.show', :default => 'Show'), url %>
24
+ <%= link_to t('button.edit', :default => 'Edit'), url + '/edit' %>
25
+ <%= link_to t('button.delete', :default => 'Delete'), url,
26
+ :remote => true,
27
+ :confirm => 'Are you sure?', :method => :delete %>
28
+ </td>
29
+ </tr>
30
+ <% end %>
31
+ </table>
@@ -0,0 +1,22 @@
1
+ <%
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 class="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>
18
+ <p style="float:right">
19
+ <%= render '/rows/submit_part', :content => :submit_right, :names => right %>
20
+ </p>
21
+ <div style="clear:both"> </div>
22
+ </div>
@@ -0,0 +1,4 @@
1
+ <%= content_for(content) %>
2
+ <% (names || []).each do |name| %>
3
+ <input type="submit" name="commit" value="<%=name%>" />
4
+ <% end %>
@@ -0,0 +1 @@
1
+ $('#row_<%= resource.id %>').fadeOut(500, function() { $(this).remove(); });
@@ -0,0 +1,22 @@
1
+ <%
2
+ # Template for RowsController (edit)
3
+ # Requires resource model_name model_symbol
4
+ # partial 'form' template
5
+
6
+ content_for(:submit_left) { link_to t('button.back', :default => 'Back'),
7
+ "/#{controller_name}" }
8
+ %>
9
+
10
+ <%= render '/shared/error_explanation' %>
11
+
12
+ <fieldset class="wide mask" id="<%= model_symbol %>">
13
+ <legend>
14
+ <%= t('ui.edit', :model => model_name,
15
+ :default => 'Edit %{model}') %>
16
+ </legend>
17
+ <%= form_for resource do |f| %>
18
+ <%= render 'form', :f => f %>
19
+ <%= render '/rows/submit',
20
+ :right => [t('ui.update', :default => 'Update')] %>
21
+ <% end %>
22
+ </fieldset>
@@ -0,0 +1,20 @@
1
+ <%
2
+ # /home/dk/docu/app/views/restful/index.html.erb
3
+
4
+ # Template for RestfulController (index)
5
+ # Requires resources model_name
6
+
7
+ if resources.length > 0
8
+ %>
9
+ <h1> <%= t('ui.listing', :model => model_name,
10
+ :default => 'Listing %{model}') %></h1>
11
+ <%= render '/shared/error_explanation' %>
12
+ <%= render '/rows/list', :resources => resources %>
13
+ <% else %>
14
+ <h1> <%= t('ui.empty_list', :model => model_name,
15
+ :default => 'List %{model} is empty') %></h1>
16
+ <% end %>
17
+
18
+ <br/>
19
+ <%= link_to t('ui.create', :default => 'Create'),
20
+ url_for(:action => :new) %>
@@ -0,0 +1,22 @@
1
+ <%
2
+ # Template for RowsController (new)
3
+ # Requires resource model_name model_symbol
4
+ # partial 'form' template
5
+
6
+ content_for(:submit_left) { link_to t('button.back', :default => 'Back'),
7
+ "/#{controller_name}" }
8
+ %>
9
+
10
+ <%= render '/shared/error_explanation' %>
11
+
12
+ <fieldset class="wide mask" id="<%= model_symbol %>">
13
+ <legend>
14
+ <%= t('ui.new', :model => model_name,
15
+ :default => 'New %{model}') %>
16
+ </legend>
17
+ <%= form_for resource do |f| %>
18
+ <%= render 'form', :f => f %>
19
+ <%= render '/rows/submit',
20
+ :right => [t('ui.create', :default => 'Create')] %>
21
+ <% end %>
22
+ </fieldset>
@@ -0,0 +1,25 @@
1
+ <%
2
+ # /home/dk/docu/app/views/restful/show.html.erb
3
+ # Template for RowsController (show)
4
+ # Requires resource model_name model_symbol
5
+ # partial 'form' template
6
+
7
+ content_for(:submit_left) { link_to t('button.edit', :default => 'Edit'),
8
+ url_for(:action => :edit) }
9
+ content_for(:submit_left) { ' | ' }
10
+ content_for(:submit_left) { link_to t('button.back', :default => 'Back'),
11
+ url_for(:action => :index) }
12
+ %>
13
+
14
+ <%= render '/shared/error_explanation' %>
15
+
16
+ <fieldset class="show wide mask" id="<%= model_symbol %>" >
17
+ <legend>
18
+ <%= t('ui.show', :model => model_name,
19
+ :default => 'Show %{model}') %>
20
+ </legend>
21
+ <%= form_for resource do |f| %>
22
+ <%= render 'form', :f => f %>
23
+ <%= render '/rows/submit' %>
24
+ <% end %>
25
+ </fieldset>
@@ -0,0 +1,14 @@
1
+ <%
2
+ # Requires resource
3
+ %>
4
+ <% if resource && resource.respond_to?(:errors) && resource.errors.any? %>
5
+ <div id="error_explanation">
6
+ <h2><%= pluralize(resource.errors.count, "error") %> prohibited this template from being saved:</h2>
7
+
8
+ <ul>
9
+ <% resource.errors.full_messages.each do |msg| %>
10
+ <li><%= msg %></li>
11
+ <% end %>
12
+ </ul>
13
+ </div>
14
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <% if notice %>
2
+ <p class="notice"> <%= notice %> </p>
3
+ <script type="text/javascript">
4
+ $('.notice').fadeOut(5000, function() { $(this).remove(); });
5
+ </script>
6
+ <% end %>
7
+ <% if alert %>
8
+ <p class="alert"> <%= alert %> </p>
9
+ <% end %>
@@ -0,0 +1,5 @@
1
+ require 'rows_controller/engine.rb'
2
+
3
+ require 'active_support/i18n'
4
+ I18n.load_path << File.expand_path('../rows_controller/locales/en.yml', __FILE__)
5
+ I18n.reload!
@@ -0,0 +1,4 @@
1
+ module Rows
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,33 @@
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
+
14
+ 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.'
22
+
23
+ create: 'Create'
24
+ update: 'Update'
25
+
26
+ button:
27
+ back: 'Back'
28
+ edit: 'Edit'
29
+ delete: 'Delete'
30
+ show: 'Show'
31
+
32
+ header:
33
+ # to_s: 'Test'
@@ -0,0 +1,3 @@
1
+ module Rows
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'rows_controller/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "rows_controller"
8
+ s.version = Rows::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = "Rows DRYs your controllers."
11
+ s.description = "YourController < RowsController ( < ApplicationController)."
12
+ s.authors = ['Dittmar Krall']
13
+ s.email = ['dittmar.krall@matique.de']
14
+ s.homepage = 'http://matique.de'
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ # s.add_development_dependency "rspec"
22
+ # s.add_runtime_dependency "rest-client"
23
+
24
+ s.add_development_dependency "sqlite3" # for testing
25
+ s.add_development_dependency "rspec-rails"
26
+ s.add_development_dependency "capybara"
27
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe OrdersController, ':' do
4
+ Order.delete_all
5
+ Order.create :name => 'name', :qty => 123
6
+ let(:order) {Order.all.first}
7
+
8
+ %w{index new}.each do |action|
9
+ describe "#{action}" do
10
+ it "renders the '#{action}' template" do
11
+ get action.to_sym
12
+ response.code.should eq('200')
13
+ response.should render_template("rows/#{action}")
14
+ response.body.should == ''
15
+ end
16
+ end
17
+ end
18
+
19
+ %w{edit show}.each do |action|
20
+ describe "#{action}" do
21
+ it "renders the '#{action}' template" do
22
+ get action.to_sym, :id => order.id
23
+ response.code.should eq('200')
24
+ response.should render_template("rows/#{action}")
25
+ response.body.should == ''
26
+ end
27
+ end
28
+ end
29
+
30
+ it 'checking resource' do
31
+ get :show, :id => order.id
32
+ subject.send(:resource).should == order
33
+ assigns(:order).should == order
34
+ end
35
+
36
+ it 'checking resources' do
37
+ get :index
38
+ subject.send(:resources).should be_a_kind_of(Array)
39
+ assigns(:orders).should be_a_kind_of(Array)
40
+ assigns(:orders).should == Order.all
41
+ end
42
+
43
+ it 'checking model_class' do
44
+ get :show, :id => order.id
45
+ subject.send(:model_class).should == Order
46
+ subject.send(:model_name).should == 'Order'
47
+ subject.send(:model_symbol).should == 'order'
48
+ subject.send(:model_symbol_plural).should == 'orders'
49
+ end
50
+ end
51
+
52
+
53
+ class CategoriesController < RowsController
54
+ model_class Order
55
+ end
56
+
57
+ describe CategoriesController do
58
+ it 'checking model_class' do
59
+ get :index
60
+ subject.send(:model_class).should == Order
61
+ subject.send(:model_name).should == 'Order'
62
+ subject.send(:model_symbol).should == 'order'
63
+ subject.send(:model_symbol_plural).should == 'orders'
64
+ end
65
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe RowsController do
4
+ specify { should respond_to(:resource) }
5
+ specify { should respond_to(:resources) }
6
+ specify { should respond_to(:model_class) }
7
+ specify { should respond_to(:model_name) }
8
+ specify { should respond_to(:model_symbol) }
9
+ specify { should respond_to(:model_symbol_plural) }
10
+ specify { should respond_to(:resources_format) }
11
+
12
+ specify { should respond_to(:index) }
13
+ specify { should respond_to(:show) }
14
+ specify { should respond_to(:new) }
15
+ specify { should respond_to(:edit) }
16
+ specify { should respond_to(:create) }
17
+ specify { should respond_to(:update) }
18
+ specify { should respond_to(:destroy) }
19
+ end