Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/plataformatec/show_for
- Loading branch information
Showing
10 changed files
with
135 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
== 0.1.2 | ||
|
||
* enhancements | ||
* allow f.attribute :nickname, :in => :profile as association shortcut | ||
|
||
* deprecations | ||
* :method now becomes :using | ||
|
||
== 0.1.1 | ||
|
||
* enhancements | ||
* HAML compatibility (by github.com/grimen) | ||
* blank_content support (by github.com/grimen) | ||
|
||
== 0.1 | ||
|
||
* First release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module ShowFor | ||
module Association | ||
def attribute(attribute_name, options={}, &block) | ||
if association_name = options.delete(:in) | ||
options[:using] = attribute_name | ||
association(association_name, options, &block) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def association(association_name, options={}, &block) | ||
apply_default_options!(association_name, options) | ||
|
||
# If a block with an iterator was given, no need to calculate the labels | ||
# since we want the collection to be yielded. Otherwise, calculate the values. | ||
value = if collection_block?(block) | ||
collection_block = block | ||
@object.send(association_name) | ||
elsif block | ||
block | ||
else | ||
association = @object.send(association_name) | ||
values = retrieve_values_from_association(association, options) | ||
|
||
if options.delete(:to_sentence) | ||
values.to_sentence | ||
elsif joiner = options.delete(:join) | ||
values.join(joiner) | ||
else | ||
values | ||
end | ||
end | ||
|
||
wrap_label_and_content(association_name, value, options, &collection_block) | ||
end | ||
|
||
protected | ||
|
||
def retrieve_values_from_association(association, options) #:nodoc: | ||
sample = association.is_a?(Array) ? association.first : association | ||
|
||
if options[:method] | ||
options[:using] = options.delete(:method) | ||
ActiveSupport::Deprecation.warn ":method is deprecated. Please use :using instead", caller | ||
end | ||
|
||
method = options.delete(:using) || ShowFor.association_methods.find { |m| sample.respond_to?(m) } | ||
association.is_a?(Array) ? association.map(&method) : association.try(method) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module ShowFor | ||
module Attribute | ||
def attribute(attribute_name, options={}, &block) | ||
apply_default_options!(attribute_name, options) | ||
collection_block, block = block, nil if collection_block?(block) | ||
|
||
value = if block | ||
block | ||
elsif @object.respond_to?(:"human_#{attribute_name}") | ||
@object.send :"human_#{attribute_name}" | ||
else | ||
@object.send(attribute_name) | ||
end | ||
|
||
wrap_label_and_content(attribute_name, value, options, &collection_block) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
en: | ||
show_for: | ||
"yes": 'Yes' | ||
"no": 'No' | ||
blank: "Not specified" | ||
"yes": "Yes" | ||
"no": "No"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module ShowFor | ||
VERSION = "0.1".freeze | ||
VERSION = "0.1.2".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters