Skip to content

Commit

Permalink
Merge pull request #912 from hardbap/rails-3.0-fix-903
Browse files Browse the repository at this point in the history
Rails 3.0 fix for 903
  • Loading branch information
sferik committed Jan 4, 2012
2 parents 613142d + 10cdee1 commit 4af3fc4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/models/rails_admin/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ 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)
Expand All @@ -20,7 +20,7 @@ def self.get_history_for_dates(mstart, mstop, ystart, ystop)
if mstart > mstop && mstart < 12
results = History.find_by_sql(["select count(*) as record_count, year, month from rails_admin_histories where month IN (?) and year = ? group by year, month",
((mstart + 1)..12).to_a, ystart])
results_two = History.find_by_sql(["select count(*) as number, year, month from rails_admin_histories where month IN (?) and year = ? group by year, month",
results_two = History.find_by_sql(["select count(*) as record_count, year, month from rails_admin_histories where month IN (?) and year = ? group by year, month",
(1..mstop).to_a, ystop])

results.concat(results_two)
Expand Down
10 changes: 10 additions & 0 deletions spec/requests/history/rails_admin_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
end
end

context "when a range does not start in December" do
context "and the start month is greater than stop month" do
it "does not produce SQL with invalid count column name" do
RailsAdmin::History.should_receive(:find_by_sql).with(["select count(*) as record_count, year, month from rails_admin_histories where month IN (?) and year = ? group by year, month", [9, 10, 11, 12], 2011]).and_return([])
RailsAdmin::History.should_receive(:find_by_sql).with(["select count(*) as record_count, year, month from rails_admin_histories where month IN (?) and year = ? group by year, month", [1], 2012]).and_return([])
RailsAdmin::History.get_history_for_dates(8, 1, 2011, 2012)
end
end
end

describe "when range starts in December" do
it "does not produce SQL with empty IN () range" do
RailsAdmin::History.should_receive(:find_by_sql).with(["select count(*) as record_count, year, month from rails_admin_histories where month IN (?) and year = ? group by year, month", [1, 2, 3, 4], 2011]).and_return([])
Expand Down

0 comments on commit 4af3fc4

Please sign in to comment.