Skip to content
Browse files

Truncate history messages longer than 255 chars. Fixes sferik/rails_a…

  • Loading branch information...
1 parent 55aa1cf commit fafb3d803de8ec326f30120154bbb2c6d87217e6 @gunn committed
Showing with 22 additions and 0 deletions.
  1. +8 −0 app/models/rails_admin/history.rb
  2. +14 −0 spec/models/rails_admin/history_spec.rb
View
8 app/models/rails_admin/history.rb
@@ -7,6 +7,14 @@ class History < ActiveRecord::Base
scope :most_recent, lambda {|model|
where("#{retrieve_connection.quote_column_name(:table)} = ?", model.pretty_name).order("updated_at DESC")
}
+
+ before_save :truncate_message
+
+ def truncate_message
+ if message.present? && message.size > 255
+ self.message = message.truncate(255)
+ end
+ end
def self.get_history_for_dates(mstart, mstop, ystart, ystop)
if mstart > mstop && mstart < 12
View
14 spec/models/rails_admin/history_spec.rb
@@ -10,4 +10,18 @@
@results.count.should == 5
end
end
+
+ describe "creating a new history entry" do
+ it "saves up to 255 chars without modification" do
+ history = RailsAdmin::History.create(:message => "#"*255).reload
+
+ history.message.should == "#"*255
+ end
+
+ it "messages longer than 255 chars are truncated" do
+ history = RailsAdmin::History.create(:message => "#"*280).reload
+
+ history.message.should == ("#"*(255-3))+"..."
+ end
+ end
end

0 comments on commit fafb3d8

Please sign in to comment.
Something went wrong with that request. Please try again.