Skip to content

Commit

Permalink
Add readonly component. Closes heartcombo#378
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Mendonça França committed Dec 5, 2011
1 parent 55a8aa6 commit cc5fd25
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

### bug fix
* Fix i18n lookup with attributes with same name of models.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/simple_form/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions lib/simple_form/components/readonly.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/simple_form/helpers/readonly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Readonly
private

def readonly_class
'readonly' if has_readonly?
:readonly if has_readonly?
end

def has_readonly?
Expand Down
1 change: 1 addition & 0 deletions lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/inputs/readonly_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -122,6 +123,10 @@ def errors
)
end
end

def self.readonly_attributes
[:credit_card]
end
end

class ValidatingUser < User
Expand Down

0 comments on commit cc5fd25

Please sign in to comment.