Skip to content

Commit 0670abe

Browse files
committed
Conflicts: lib/active_admin/views/components/table_for.rb spec/unit/views/components/table_for_spec.rb
2 parents 19abd8f + 750dc56 commit 0670abe

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

features/index/index_as_table.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ Feature: Index as Table
168168
When I am on the index page for posts
169169
Then I should see the "index_table_posts" table:
170170
| [ ] | Id | Title | Body | Published At | Starred | Created At | Updated At | |
171-
| [ ] | 2 | Bye bye world | Move your... | | | /.*/ | /.*/ | ViewEditDelete |
172-
| [ ] | 1 | Hello World | From the body | | | /.*/ | /.*/ | ViewEditDelete |
171+
| [ ] | 2 | Bye bye world | Move your... | | No | /.*/ | /.*/ | ViewEditDelete |
172+
| [ ] | 1 | Hello World | From the body | | No | /.*/ | /.*/ | ViewEditDelete |
173173
When I follow "Id"
174174
Then I should see the "index_table_posts" table:
175175
| [ ] | Id | Title | Body | Published At | Starred | Created At | Updated At | |
176-
| [ ] | 1 | Hello World | From the body | | | /.*/ | /.*/ | ViewEditDelete |
177-
| [ ] | 2 | Bye bye world | Move your... | | | /.*/ | /.*/ | ViewEditDelete |
176+
| [ ] | 1 | Hello World | From the body | | No | /.*/ | /.*/ | ViewEditDelete |
177+
| [ ] | 2 | Bye bye world | Move your... | | No | /.*/ | /.*/ | ViewEditDelete |
178178

179179
Scenario: Sorting by a virtual column
180180
Given a post with the title "Hello World" exists

lib/active_admin/views/components/status_tag.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def build(*args)
3636
type = args[1]
3737
label = options.delete(:label)
3838
classes = options.delete(:class)
39+
status = convert_to_boolean_status(status)
3940

4041
content = label || status.titleize if status
4142

@@ -48,6 +49,16 @@ def build(*args)
4849

4950
protected
5051

52+
def convert_to_boolean_status(status)
53+
if status == 'true'
54+
'Yes'
55+
elsif ['false', nil].include?(status)
56+
'No'
57+
else
58+
status
59+
end
60+
end
61+
5162
def status_to_class(status)
5263
status.titleize.gsub(/\s/, '').underscore
5364
end

lib/active_admin/views/components/table_for.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,16 @@ def render_data(data, item)
9494
item[data]
9595
end
9696
value = pretty_format(value) if data.is_a?(Symbol)
97+
value = status_tag value if is_boolean? data, item
9798
value
9899
end
99100

101+
# Returns true or false depending on the attr being boolean
102+
def is_boolean?(data, item)
103+
attr = item.column_for_attribute data
104+
attr.present? && attr.type == :boolean
105+
end
106+
100107
# Returns an array for the current sort order
101108
# current_sort[0] #=> sort_key
102109
# current_sort[1] #=> asc | desc

spec/unit/views/components/status_tag_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,19 @@ def status_tag(*args)
4646
its(:class_list) { should include('status_tag') }
4747
its(:content) { should eq '' }
4848
end
49+
50+
context "when status is false" do
51+
subject { status_tag('false') }
52+
53+
its(:class_list) { should include('status_tag') }
54+
its(:content) { should == 'No' }
55+
end
4956

5057
context "when status is nil" do
5158
subject { status_tag(nil) }
5259

5360
its(:class_list) { should include('status_tag') }
54-
its(:content) { should eq '' }
61+
its(:content) { should == 'No' }
5562
end
5663

5764
context "when status is 'Active' and type is :ok" do

spec/unit/views/components/table_for_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
describe "creating with the dsl" do
55

66
let(:collection) do
7-
[Post.new(:title => "First Post"), Post.new(:title => "Second Post"), Post.new(:title => "Third Post")]
7+
[Post.new(:title => "First Post", :starred => true), Post.new(:title => "Second Post"), Post.new(:title => "Third Post", :starred => false)]
88
end
99

1010
let(:assigns){ { :collection => collection } }
@@ -179,6 +179,25 @@
179179
end
180180
end
181181

182+
context "when record attribute is boolean" do
183+
let(:table) do
184+
render_arbre_component assigns, helpers do
185+
table_for(collection) do
186+
column :starred
187+
end
188+
end
189+
end
190+
191+
it "should render boolean attribute within status tag" do
192+
table.find_by_tag("span").first
193+
.class_list.to_a.join(' ').should == "status_tag yes"
194+
table.find_by_tag("span").first.content.should == "Yes"
195+
table.find_by_tag("span").last
196+
.class_list.to_a.join(' ').should == "status_tag no"
197+
table.find_by_tag("span").last.content.should == "No"
198+
end
199+
end
200+
182201
end
183202

184203
describe "column sorting" do

0 commit comments

Comments
 (0)