Permalink
Browse files

Cleaned up the code and specs around the validation_reflection, inclu…

…ding documentation ion the README, etc
  • Loading branch information...
1 parent 827d9ac commit b03d5b70727d93d72671b5c008177337890d4e4a @justinfrench committed Nov 2, 2008
Showing with 43 additions and 23 deletions.
  1. +4 −0 README.textile
  2. +23 −15 lib/justin_french/formtastic.rb
  3. +16 −8 spec/formtastic_spec.rb
View
@@ -165,6 +165,10 @@ end
</pre>
+h2. Dependencies
+
+None! But if you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin is installed, you won't have to specify the :required option (it checks the validations on the model instead).
+
h2. What about Stylesheets?
@@ -105,7 +105,7 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
def input(method, options = {})
raise NoMethodError unless @object.respond_to?(method)
- options[:required] = required(method, options[:required])
+ options[:required] = method_required?(method, options[:required])
options[:label] ||= method.to_s.humanize
options[:as] ||= default_input_type(@object, method)
input_method = "#{options[:as]}_input"
@@ -117,19 +117,6 @@ def input(method, options = {})
return @template.content_tag(:li, content, list_item_html_attributes(method, options))
end
- def required(attribute, required_option)
- if @object.class.method_defined?(:reflect_on_all_validations)
- attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym
- @object.class.reflect_on_all_validations.any? do |validation|
- validation.macro == :validates_presence_of && validation.name == attribute_sym
- end
- elsif required_option.nil?
- @@all_fields_required_by_default
- else
- required_option
- end
- end
-
# Creates a fieldset and ol tag wrapping for form inputs as list items. Example:
#
# <% form_for @user do |form| %>
@@ -194,12 +181,33 @@ def error_messages
protected
-
def save_or_create_commit_button_text #:nodoc:
prefix = @object.new_record? ? "Create" : "Save"
"#{prefix} #{@object_name.humanize}"
end
+ # Determins if the attribute (eg :title) should be considered required or not.
+ #
+ # * if the :required option was provided in the options hash, the true/false value will be
+ # returned immediately, allowing the view to override any guesswork that follows:
+ # * if the :required option isn't provided in the options hash, and the ValidationReflection
+ # plugin is installed (http://github.com/redinger/validation_reflection), true is returned
+ # if the validates_presence_of macro has been used in the class for this attribute, or false
+ # otherwise.
+ # * if the :required option isn't provided, and the plugin isn't available, the value of the
+ # configuration option @@all_fields_required_by_default is used.
+ def method_required?(attribute, required_option) #:nodoc:
+ return required_option unless required_option.nil?
+
+ if @object.class.method_defined?(:reflect_on_all_validations)
+ attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym
+ @object.class.reflect_on_all_validations.any? do |validation|
+ validation.macro == :validates_presence_of && validation.name == attribute_sym
+ end
+ else
+ @@all_fields_required_by_default
+ end
+ end
# Outputs a label and a select box containing options from the parent (belongs_to) association.
#
@@ -165,6 +165,10 @@ class Author; end
describe 'when true' do
+ before do
+ @new_post.class.should_not_receive(:reflect_on_all_validations)
+ end
+
it 'should set a "required" class' do
_erbout = ''
semantic_form_for(@new_post) do |builder|
@@ -187,6 +191,10 @@ class Author; end
describe 'when false' do
+ before do
+ @new_post.class.should_not_receive(:reflect_on_all_validations)
+ end
+
it 'should set an "optional" class' do
_erbout = ''
semantic_form_for(@new_post) do |builder|
@@ -210,15 +218,17 @@ class Author; end
describe 'when not provided' do
describe 'and the validation reflection plugin is available' do
-
+
+ before do
+ @new_post.class.stub!(:method_defined?).with(:reflect_on_all_validations).and_return(true)
+ end
+
describe 'and validates_presence_of was called for the method' do
before do
- @new_post.class.stub!(:reflect_on_all_validations).and_return([
+ @new_post.class.should_receive(:reflect_on_all_validations).and_return([
mock('MacroReflection', :macro => :validates_presence_of, :name => :title)
])
- @new_post.class.stub!(:method_defined?).with(:reflect_on_all_validations).and_return(true)
- @new_post.class.should_receive(:reflect_on_all_validations)
end
it 'should be required' do
@@ -235,11 +245,9 @@ class Author; end
describe 'and validates_presence_of was not called for the method' do
before do
- @new_post.class.stub!(:reflect_on_all_validations).and_return([])
- @new_post.class.stub!(:method_defined?).with(:reflect_on_all_validations).and_return(true)
- @new_post.class.should_receive(:reflect_on_all_validations)
+ @new_post.class.should_receive(:reflect_on_all_validations).and_return([])
end
-
+
it 'should not be required' do
_erbout = ''
semantic_form_for(@new_post) do |builder|

0 comments on commit b03d5b7

Please sign in to comment.