dry_crud 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -17,8 +17,7 @@ Don't worry, it's a simple development dependency. You may even savely remove it
17
17
  To integrate DRY CRUD into your code, only a few additions are required:
18
18
 
19
19
  * For uniform CRUD functionality, just subclass your controllers from +CrudController+.
20
- * To use standard formatting, tables and forms throughout your application, add <tt>helper :standard</tt> to your +ApplicationController+ and benefit everywhere from these little helper methods.
21
- * Overwrite the <tt>:to_s</tt> method of your models for a human-friendly representation.
20
+ * Overwrite the <tt>:to_s</tt> method of your models for a human-friendly representation in captions.
22
21
 
23
22
  Version 1.0 and higher are built for Rails 3. If you need a version for Rails 2.3, please get version 0.6.0 of the gem or go to the rails-2.3 branch on Github. DRY CRUD 1.3 is fully compatible with Ruby 1.8.7, Ruby 1.9.2 and JRuby.
24
23
 
@@ -38,6 +37,8 @@ DRY CRUD is a Rails generator. All code resides in your application and is open
38
37
 
39
38
  DRY CRUD does not depend on any other plugins, but easily allows you to integrate them in order to unify the behavior of your CRUD controllers. You might even use the plugins mentioned above to adapt your generated +CrudController+ base class. All classes come with thorough tests that provide you with a solid foundation for implementing your own adaptions.
40
39
 
40
+ If you find yourself adapting the same parts of DRY CRUD for your applications over and over, please feel free to {fork me on Github}[http://github.com/codez/dry_crud].
41
+
41
42
  See the Examples section for some use cases and the Generated Files section below for details on the single classes and templates.
42
43
 
43
44
  == Examples
@@ -93,12 +94,14 @@ In this case, you should also pass the parameter <tt>page=1</tt> with a hidden f
93
94
 
94
95
  ==== Special formatting for selected attributes
95
96
 
96
- Sometimes, the default formatting provided by <tt>:format_attr</tt> will not be sufficient. We have a boolean column +sex+ in our model, but would like to display 'male' or 'female' for it (instead of 'no' or 'yes', which is a bit cryptic). Just define a method in your view helper starting with <tt>format_</tt>, followed by the attribute name:
97
+ Sometimes, the default formatting provided by <tt>:format_attr</tt> will not be sufficient. We have a boolean column +sex+ in our model, but would like to display 'male' or 'female' for it (instead of 'no' or 'yes', which is a bit cryptic). Just define a method in your view helper starting with <tt>format_</tt>, followed by the class and attribute name:
97
98
 
98
99
  In <tt>app/helpers/people.rb</tt>:
99
- def format_sex(person)
100
+ def format_person_sex(person)
100
101
  person.sex ? 'female' : 'male'
101
102
  end
103
+
104
+ Should you have attributes with the same name for multiple models that you want to be formatted the same way, you may define a helper method <tt>format_{attr}</tt> for these attributes.
102
105
 
103
106
  By the way: The method <tt>:f</tt> in +StandardHelper+ uniformly formats arbitrary values according to their class.
104
107
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0
1
+ 1.3.1
@@ -5,9 +5,6 @@ required:
5
5
 
6
6
  * For uniform CRUD functionallity, just subclass your controllers from
7
7
  CrudController.
8
- * To use standard formatting, tables and forms throughout your
9
- application, add 'helper :standard' to your ApplicationController and
10
- benefit everywhere from these little helper methods.
11
8
  * Overwrite the :to_s method of your models for a human-friendly
12
9
  representation.
13
10
 
@@ -8,9 +8,6 @@ class ListController < ApplicationController
8
8
 
9
9
  include RenderInheritable
10
10
 
11
- # Move this declaration to the application controller.
12
- helper :standard
13
-
14
11
  helper_method :model_class, :models_label
15
12
 
16
13
  delegate :model_class, :models_label, :to => 'self.class'
@@ -14,16 +14,10 @@ class StandardFormBuilder < ActionView::Helpers::FormBuilder
14
14
 
15
15
  # Render multiple input fields together with a label for the given attributes.
16
16
  def labeled_input_fields(*attrs)
17
- attrs.collect {|a| labeled_input_field(a) }.join("\n").html_safe
18
- end
19
-
20
- # Render a standartized label.
21
- def label(attr, text = nil, options = {})
22
- if attr.is_a?(Symbol) && text.nil? && options.blank?
23
- super(attr, captionize(attr, @object.class))
24
- else
25
- super(attr, text, options)
26
- end
17
+ options = attrs.extract_options!
18
+ attrs.collect do |a|
19
+ labeled_input_field(a, options.clone)
20
+ end.join("\n").html_safe
27
21
  end
28
22
 
29
23
  # Render a corresponding input field for the given attribute.
@@ -47,45 +41,34 @@ class StandardFormBuilder < ActionView::Helpers::FormBuilder
47
41
  end
48
42
  end
49
43
 
50
- # Render a standard text field.
51
- def text_field(attr, html_options = {})
52
- super(attr, {:size => 30}.merge(html_options))
53
- end
54
-
55
- # Render a standard password field.
56
- def password_field(attr, html_options = {})
57
- super(attr, {:size => 30}.merge(html_options))
58
- end
59
-
60
- # Render a standard text area.
61
- def text_area(attr, html_options = {})
62
- super(attr, {:rows => 5, :cols => 30}.merge(html_options))
44
+ # Render a number field.
45
+ def number_field(attr, html_options = {})
46
+ html_options[:size] ||= 10
47
+ super(attr, html_options)
63
48
  end
64
-
49
+
65
50
  # Render a standard string field with column contraints.
66
51
  def string_field(attr, html_options = {})
67
- limit = column_property(@object, attr, :limit)
68
- html_options = {:maxlength => limit}.merge(html_options) if limit
52
+ html_options[:maxlength] ||= column_property(@object, attr, :limit)
53
+ html_options[:size] ||= 30
69
54
  text_field(attr, html_options)
70
55
  end
71
56
 
72
- # Render a standard number field.
73
- def number_field(attr, html_options = {})
74
- super(attr, {:size => 15}.merge(html_options))
75
- end
76
-
77
57
  # Render an integer field.
78
58
  def integer_field(attr, html_options = {})
59
+ html_options[:step] ||= 1
79
60
  number_field(attr, html_options)
80
61
  end
81
62
 
82
63
  # Render a float field.
83
64
  def float_field(attr, html_options = {})
65
+ html_options[:step] ||= 'any'
84
66
  number_field(attr, html_options)
85
67
  end
86
68
 
87
69
  # Render a decimal field.
88
70
  def decimal_field(attr, html_options = {})
71
+ html_options[:step] ||= 'any'
89
72
  number_field(attr, html_options)
90
73
  end
91
74
 
@@ -141,7 +124,7 @@ class StandardFormBuilder < ActionView::Helpers::FormBuilder
141
124
  end
142
125
 
143
126
  # Dispatch methods starting with 'labeled_' to render a label and the corresponding
144
- # input field. E.g. labeled_boolean_field(:checked, {:class => 'bold'})
127
+ # input field. E.g. labeled_boolean_field(:checked, :class => 'bold')
145
128
  def method_missing(name, *args)
146
129
  if field_method = labeled_field_method?(name)
147
130
  labeled(args.first, send(field_method, *args) + required_mark(args.first))
@@ -150,9 +133,11 @@ class StandardFormBuilder < ActionView::Helpers::FormBuilder
150
133
  end
151
134
  end
152
135
 
136
+ # Overriden to fullfill contract with method_missing 'labeled_' methods.
153
137
  def respond_to?(name)
154
138
  labeled_field_method?(name).present? || super(name)
155
139
  end
140
+
156
141
  protected
157
142
 
158
143
  # Returns true if attr is a non-polymorphic belongs_to association,
@@ -7,14 +7,11 @@ module StandardHelper
7
7
 
8
8
  ################ FORMATTING HELPERS ##################################
9
9
 
10
- # Define an array of associations symbols in your helper that should not get automatically linked.
11
- #def no_assoc_links = [:city]
12
-
13
10
  # Formats a single value
14
11
  def f(value)
15
12
  case value
16
13
  when Fixnum then number_with_delimiter(value)
17
- when Float then number_with_precision(value, :precision => 2)
14
+ when Float, BigDecimal then number_with_precision(value, :precision => 2)
18
15
  when Date then l(value)
19
16
  when Time then l(value, :format => :time)
20
17
  when true then t(:"global.yes")
@@ -25,12 +22,16 @@ module StandardHelper
25
22
  end
26
23
 
27
24
  # Formats an arbitrary attribute of the given ActiveRecord object.
28
- # If no specific format_{attr} method is found, formats the value as follows:
25
+ # If no specific format_{type}_{attr} or format_{attr} method is found,
26
+ # formats the value as follows:
29
27
  # If the value is an associated model, renders the label of this object.
30
28
  # Otherwise, calls format_type.
31
29
  def format_attr(obj, attr)
30
+ format_type_attr_method = :"format_#{obj.class.name.underscore}_#{attr.to_s}"
32
31
  format_attr_method = :"format_#{attr.to_s}"
33
- if respond_to?(format_attr_method)
32
+ if respond_to?(format_type_attr_method)
33
+ send(format_type_attr_method, obj)
34
+ elsif respond_to?(format_attr_method)
34
35
  send(format_attr_method, obj)
35
36
  elsif assoc = association(obj, attr, :belongs_to)
36
37
  format_assoc(obj, assoc)
@@ -61,9 +62,7 @@ module StandardHelper
61
62
  # Renders a list of attributes with label and value for a given object.
62
63
  # Optionally surrounded with a div.
63
64
  def render_attrs(obj, *attrs)
64
- attrs.collect do |a|
65
- labeled_attr(obj, a)
66
- end.join("\n").html_safe
65
+ attrs.collect { |a| labeled_attr(obj, a) }.join("\n").html_safe
67
66
  end
68
67
 
69
68
  # Renders the formatted content of the given attribute with a label.
@@ -262,7 +261,6 @@ module StandardHelper
262
261
 
263
262
  # Returns true if no link should be created when formatting the given association.
264
263
  def no_assoc_link?(assoc, val)
265
- (respond_to?(:no_assoc_links) && no_assoc_links.to_a.include?(assoc.name.to_sym)) ||
266
264
  !respond_to?("#{val.class.name.underscore}_path".to_sym)
267
265
  end
268
266
 
@@ -1,9 +1,8 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1
+ <!DOCTYPE html>
3
2
 
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
+ <html lang="en">
5
4
  <head>
6
- <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
5
+ <meta charset="utf-8" />
7
6
  <title><%= strip_tags(@title) %></title>
8
7
  <%= stylesheet_link_tag 'crud' %>
9
8
  <%= csrf_meta_tag %>
@@ -34,7 +33,7 @@
34
33
  </div>
35
34
 
36
35
  <%= javascript_include_tag :defaults, :cache => 'all' %>
37
- <%= javascript_tag content_tag_for(:javascripts) if content_for?(:javascripts) %>
36
+ <%= javascript_tag yield(:javascripts) if content_for?(:javascripts) %>
38
37
 
39
38
  </body>
40
39
  </html>
@@ -137,6 +137,24 @@ a.action img {
137
137
  vertical-align: text-top;
138
138
  }
139
139
 
140
+ input, textarea {
141
+ font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;
142
+ font-size: 10pt;
143
+ }
144
+
145
+ input[type=text], input[type=password] {
146
+ width: 180pt;
147
+ }
148
+
149
+ input[type=number] {
150
+ width: 80pt;
151
+ }
152
+
153
+ textarea {
154
+ width: 180pt;
155
+ height: 80pt;
156
+ }
157
+
140
158
  .cancel {
141
159
  font-size: 80%;
142
160
  margin-left: 7pt;
@@ -10,6 +10,10 @@ class StandardHelperTest < ActionView::TestCase
10
10
  teardown :reset_db
11
11
 
12
12
  def format_size(obj)
13
+ "#{f(obj.size)} items"
14
+ end
15
+
16
+ def format_string_size(obj)
13
17
  "#{f(obj.size)} chars"
14
18
  end
15
19
 
@@ -82,9 +86,13 @@ class StandardHelperTest < ActionView::TestCase
82
86
  assert_equal "12.23", format_attr("12.23424", :to_f)
83
87
  end
84
88
 
85
- test "format attr with custom format_size method" do
89
+ test "format attr with custom format_string_size method" do
86
90
  assert_equal "4 chars", format_attr("abcd", :size)
87
91
  end
92
+
93
+ test "format attr with custom format_size method" do
94
+ assert_equal "2 items", format_attr([1,2], :size)
95
+ end
88
96
 
89
97
  test "column types" do
90
98
  m = crud_test_models(:AAAAA)
@@ -1,6 +1,6 @@
1
1
  module PeopleHelper
2
2
 
3
- def format_income(person)
3
+ def format_person_income(person)
4
4
  income = person.income
5
5
  income.present? ? "#{f(income)} $" : StandardHelper::EMPTY_STRING
6
6
  end
@@ -2,3 +2,7 @@
2
2
 
3
3
  <%= link_action ti(:'link.ajax'), nil, {:action => 'ajax'}, :method => :get, :remote => true %>
4
4
  <div id="response"></div>
5
+
6
+ <% content_for :javascripts do %>
7
+ var sayHello = function() { alert('Hello'); };
8
+ <% end %>
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry_crud
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 3
9
- - 0
10
- version: 1.3.0
9
+ - 1
10
+ version: 1.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pascal Zumkehr
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-28 00:00:00 +02:00
18
+ date: 2011-04-26 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -93,7 +93,6 @@ files:
93
93
  - lib/generators/dry_crud/templates/test/unit/helpers/standard_table_builder_test.rb
94
94
  - lib/generators/dry_crud/USAGE
95
95
  - test/templates/app/controllers/ajax_controller.rb
96
- - test/templates/app/controllers/application_controller.rb
97
96
  - test/templates/app/controllers/cities_controller.rb
98
97
  - test/templates/app/controllers/people_controller.rb
99
98
  - test/templates/app/controllers/vips_controller.rb
@@ -1,11 +0,0 @@
1
- # Filters added to this controller apply to all controllers in the application.
2
- # Likewise, all the methods added will be available for all controllers.
3
-
4
- class ApplicationController < ActionController::Base
5
- helper :standard
6
-
7
- protect_from_forgery # See ActionController::RequestForgeryProtection for details
8
-
9
- # Scrub sensitive parameters from your log
10
- # filter_parameter_logging :password
11
- end