Permalink
Browse files

Make column max_width and min_width options friendlier

Convert sizes without a '%' or 'px' suffix to a pixel width
  • Loading branch information...
1 parent 74c1b22 commit c6bac2649c0dcad89af18d698908a685dc0e8738 @mattvague committed Jan 27, 2014
Showing with 39 additions and 6 deletions.
  1. +9 −2 lib/active_admin/views/components/columns.rb
  2. +30 −4 spec/unit/views/components/columns_spec.rb
@@ -133,18 +133,25 @@ def set_column_styles(column_width, margin_width, is_last_column = false)
styles << "width: #{column_with_span_width}%;"
if max_width
- styles << "max-width: #{max_width};"
+ styles << "max-width: #{safe_width(max_width)};"
end
if min_width
- styles << "min-width: #{min_width};"
+ styles << "min-width: #{safe_width(min_width)};"
end
styles << "margin-right: #{margin_width}%;" unless is_last_column
set_attribute :style, styles.join(" ")
end
+ private
+
+ # Converts values without a '%' or 'px' suffix to a pixel value
+ def safe_width(width)
+ width.to_s.gsub(/\A(\d+)\z/, '\1px')
+ end
+
end
end
end
@@ -112,8 +112,21 @@
expect(cols.children.first.attr(:style)).to eq "width: 49.0%; max-width: 100px; margin-right: 2%;"
end
- it "should omit the value if not presetn" do
- expect(cols.children.last.attr(:style)).to eq "width: 49.0%;"
+ it "should omit the value if not present" do
+ cols.children.last.attr(:style).should == "width: 49.0%;"
+ end
+
+ context "when passed an integer value" do
+ let(:cols) do
+ columns do
+ column(:max_width => 100){ "Hello World" }
+ column(){ "Hello World" }
+ end
+ end
+
+ it "should be treated as pixels" do
+ cols.children.first.attr(:style).should == "width: 49.0%; max-width: 100px; margin-right: 2%;"
+ end
end
end
@@ -133,8 +146,21 @@
expect(cols.children.first.attr(:style)).to eq "width: 49.0%; min-width: 100px; margin-right: 2%;"
end
- it "should omit the value if not presetn" do
- expect(cols.children.last.attr(:style)).to eq "width: 49.0%;"
+ it "should omit the value if not present" do
+ cols.children.last.attr(:style).should == "width: 49.0%;"
+ end
+
+ context "when passed an integer value" do
+ let(:cols) do
+ columns do
+ column(:min_width => 100){ "Hello World" }
+ column(){ "Hello World" }
+ end
+ end
+
+ it "should be treated as pixels" do
+ cols.children.first.attr(:style).should == "width: 49.0%; min-width: 100px; margin-right: 2%;"
+ end
end
end

0 comments on commit c6bac26

Please sign in to comment.