Permalink
Browse files

fixing admin_rails with this fix: https://gist.github.com/951122

  • Loading branch information...
1 parent 68bff45 commit b340a0cee6c45fba011c05c96a0b6d8c4462aa2d @blueroot blueroot committed May 16, 2011
Showing with 50 additions and 0 deletions.
  1. +1 −0 Gemfile
  2. +1 −0 Gemfile.lock
  3. +48 −0 config/initializers/rails_admin.rb
View
@@ -1,5 +1,6 @@
source :rubygems
+gem 'rake'
gem 'mysql', '2.8.1'
gem 'rails', '3.0.3'
gem 'jquery-rails', '0.2.5'
View
@@ -215,6 +215,7 @@ DEPENDENCIES
omniauth (= 0.1.6)
rails (= 3.0.3)
rails_admin!
+ rake
simple-navigation (= 3.0.2)
sunspot_rails (= 1.2.1)
tabletastic (= 0.2.1)
@@ -71,3 +71,51 @@
end
+module RailsAdmin
+ class History < ActiveRecord::Base
+ set_table_name :rails_admin_histories
+
+ IGNORED_ATTRS = Set[:id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
+
+ scope :most_recent, lambda {|table|
+ where("#{retrieve_connection.quote_column_name(:table)} = ?", table).order("updated_at")
+ }
+
+ def self.get_history_for_dates(mstart, mstop, ystart, ystop)
+ sql_in = ""
+ if mstart > mstop
+ # fix by Dan Choi
+ #sql_in = (mstart + 1..12).to_a.join(", ") <== possible culprit May month bug
+ sql_in = (mstart..12).to_a.join(", ")
+ sql_in_two = (1..mstop).to_a.join(", ")
+
+ results = History.find_by_sql("select count(*) as number, year, month from rails_admin_histories where month IN (#{sql_in}) and year = #{ystart} group by year, month")
+ results_two = History.find_by_sql("select count(*) as number, year, month from rails_admin_histories where month IN (#{sql_in_two}) and year = #{ystop} group by year, month")
+
+ results.concat(results_two)
+ else
+ #sql_in = (mstart + 1..mstop).to_a.join(", ") <=== may be defective too
+ sql_in = (mstart..mstop).to_a.join(", ")
+
+ results = History.find_by_sql("select count(*) as number, year, month from rails_admin_histories where month IN (#{sql_in}) and year = #{ystart} group by year, month")
+ end
+
+ results.each do |result|
+ result.number = result.number.to_i
+ end
+
+ add_blank_results(results, mstart, ystart)
+ end
+
+ def self.add_blank_results(results, mstart, ystart)
+ # fill in an array with BlankHistory
+ blanks = Array.new(5) { |i| BlankHistory.new(((mstart+i) % 12)+1, ystart + ((mstart+i)/12)) }
+ # replace BlankHistory array entries with the real History entries that were provided
+ blanks.each_index do |i|
+ if results[0] && results[0].year == blanks[i].year && results[0].month == blanks[i].month
+ blanks[i] = results.delete_at 0
+ end
+ end
+ end
+ end
+end

0 comments on commit b340a0c

Please sign in to comment.