Skip to content

Commit

Permalink
Should support null value from content
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnHarris committed Apr 8, 2016
1 parent 513cf3e commit 0424bd6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/x-editable-rails/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,9 @@ def format_source(source, value)
if source.is_a?(Array) && source.first.is_a?(String) && source.size == 2
{ '1' => source[0], '0' => source[1] }
end
when String
else
if source.is_a?(Array) && source.first.is_a?(String)
source.inject({}){|hash, key| hash.merge(key => key)}
elsif source.is_a?(Hash)
source
source.map { |v| { value: v, text: v } }
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/view_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ def initialize(attributes={})
"ViewHelpers#editable should generate content tag with url source as a data attribute"
end

test "editable should generate content tag without current value" do
subject_1 = Subject.new(content: nil)

assert_match %r{<span[^>]+></span>},
editable(subject_1, :content),
"ViewHelpers#editable should generate content tag with the current value"

assert_match %r{<span[^>]+></span>},
editable(subject_1, :content, type: "select", source: ["foo", "bar"]),
"ViewHelpers#editable should generate content tag with the current value"

assert_match %r{<span[^>]+></span>},
editable(subject_1, :content, type: "select", source: [["foo", "Foo"], ["bar", "Bar"]]),
"ViewHelpers#editable should generate content tag with the current value"

assert_match %r{<span[^>]+></span>},
editable(subject_1, :content, type: "select", source: { "foo" => "Foo", "bar" => "Bar" }),
"ViewHelpers#editable should generate content tag with the current value"

assert_match %r{<span[^>]+></span>},
editable(subject_1, :content, type: "select", source: [{ text: "Foo", value: "foo" }, { text: "Bar", value: "bar" }]),
"ViewHelpers#editable should generate content tag with the current value"
end

private

def view_helpers_test_subject_path(subject)
Expand Down

0 comments on commit 0424bd6

Please sign in to comment.