Browse files

Merge pull request #379 from plataformatec/readonly

Add readonly component
  • Loading branch information...
2 parents f36ec7b + cc5fd25 commit 6e4431f7c661b5d4f7cd8268517671acd1a99357 @josevalim josevalim committed Dec 4, 2011
View
1 CHANGELOG.md
@@ -16,6 +16,7 @@
This is not backward compatible with the previous versions of SimpleForm.
For more informations see [this comment](https://github.com/plataformatec/simple_form/issues/360#issuecomment-3000780).
([@nashby](https://github.com/nashby))
+ * Add a readonly component that does automatically readonly lookup from object
### deprecation
* Deprecate the `translate` configuration in favor of `translate_labels`
View
2 lib/generators/simple_form/templates/config/initializers/simple_form.rb
@@ -16,6 +16,8 @@
# b.use :pattern
# Calculates min and max from length validations automatically for numeric inputs
# b.use :min_max
+ # Calculates readonly automatically from the readonly attributes
+ # b.use :readonly
# Calculates placeholders automatically from I18n
b.use :placeholder
# Calculates required from presence validations automatically
View
1 lib/simple_form.rb
@@ -149,6 +149,7 @@ def self.build(options={})
b.use :placeholder
b.use :required
b.use :pattern
+ b.use :readonly
b.use :label_input
b.use :hint, :tag => :span, :class => :hint
View
1 lib/simple_form/components.rb
@@ -16,5 +16,6 @@ module Components
autoload :Pattern, 'simple_form/components/pattern'
autoload :Placeholders, 'simple_form/components/placeholders'
autoload :Required, 'simple_form/components/required'
+ autoload :Readonly, 'simple_form/components/readonly'
end
end
View
27 lib/simple_form/components/readonly.rb
@@ -0,0 +1,27 @@
+module SimpleForm
+ module Components
+ # Needs to be enabled in order to do automatic lookups.
+ module Readonly
+ def readonly
+ if readonly_attribute?
+ input_html_options[:readonly] ||= true
+ add_readonly_class!
+ end
+
+ nil
+ end
+
+ private
+
+ def readonly_attribute?
+ object.class.respond_to?(:readonly_attributes) &&
+ object.persisted? &&
+ object.class.readonly_attributes.include?(attribute_name)
+ end
+
+ def add_readonly_class!
+ input_html_classes.push(:readonly).compact! unless input_html_classes.include?(:readonly)
+ end
+ end
+ end
+end
View
2 lib/simple_form/helpers/readonly.rb
@@ -4,7 +4,7 @@ module Readonly
private
def readonly_class
- 'readonly' if has_readonly?
+ :readonly if has_readonly?
end
def has_readonly?
View
1 lib/simple_form/inputs/base.rb
@@ -18,6 +18,7 @@ class Base
include SimpleForm::Components::Pattern
include SimpleForm::Components::Placeholders
include SimpleForm::Components::Required
+ include SimpleForm::Components::Readonly
attr_reader :attribute_name, :column, :input_type, :reflection,
:options, :input_html_options, :input_html_classes
View
23 test/inputs/readonly_test.rb
@@ -35,4 +35,27 @@ class ReadonlyTest < ActionView::TestCase
with_input_for @user, :created_at, :datetime
assert_select 'select.datetime:not(.readonly[readonly])'
end
+
+ test 'input should generate readonly attribute when the field is readonly and the object is persisted' do
+ with_input_for @user, :credit_card, :string
+ assert_select 'input.string.readonly[readonly]'
+ end
+
+ test 'input should not generate readonly attribute when the field is readonly and the object is not persisted' do
+ @user.new_record!
+ with_input_for @user, :credit_card, :string
+ assert_no_select 'input.string.readonly[readonly]'
+ end
+
+ test 'input should not generate readonly attribute when the field is not readonly and the object is persisted' do
+ with_input_for @user, :name, :string
+ assert_no_select 'input.string.readonly[readonly]'
+ end
+
+ test 'input should not generate readonly attribute when the component is not used' do
+ swap_wrapper do
+ with_input_for @user, :credit_card, :string
+ assert_no_select 'input.string.readonly[readonly]'
+ end
+ end
end
View
7 test/support/models.rb
@@ -42,7 +42,7 @@ class User
:description, :created_at, :updated_at, :credit_limit, :password, :url,
:delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids,
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
- :post_count, :lock_version, :amount, :attempts, :action
+ :post_count, :lock_version, :amount, :attempts, :action, :credit_card
def initialize(options={})
options.each do |key, value|
@@ -80,6 +80,7 @@ def column_for_attribute(attribute)
when :amount then :integer
when :attempts then :integer
when :action then :string
+ when :credit_card then :string
end
Column.new(attribute, column_type, limit)
end
@@ -122,6 +123,10 @@ def errors
)
end
end
+
+ def self.readonly_attributes
+ [:credit_card]
+ end
end
class ValidatingUser < User

0 comments on commit 6e4431f

Please sign in to comment.