kaminari.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. module Kaminari
  2. def self.frameworks
  3. frameworks = []
  4. case
  5. when rails? then frameworks << 'rails'
  6. when sinatra? then frameworks << 'sinatra/base'
  7. end
  8. frameworks
  9. end
  10. def self.load_framework!
  11. show_warning if frameworks.empty?
  12. frameworks.each do |framework|
  13. begin
  14. require framework
  15. rescue NameError => e
  16. raise "Failed to load framework #{framework.inspect}. Have you added it to Gemfile?"
  17. end
  18. end
  19. end
  20. def self.show_warning
  21. $stderr.puts <<-EOC
  22. warning: no framework detected.
  23. would you check out if your Gemfile appropriately configured?
  24. ---- e.g. ----
  25. when Rails:
  26. gem 'rails'
  27. gem 'kaminari'
  28. when Sinatra/Padrino:
  29. gem 'kaminari', :require => 'kaminari/sinatra'
  30. EOC
  31. end
  32. def self.load_kaminari!
  33. require 'kaminari/config'
  34. require 'kaminari/helpers/action_view_extension'
  35. require 'kaminari/helpers/paginator'
  36. require 'kaminari/models/page_scope_methods'
  37. require 'kaminari/models/configuration_methods'
  38. end
  39. def self.hook!
  40. load_framework!
  41. load_kaminari!
  42. require 'kaminari/hooks'
  43. if rails?
  44. require 'kaminari/railtie'
  45. require 'kaminari/engine'
  46. elsif sinatra?
  47. require 'kaminari/sinatra'
  48. else
  49. Kaminari::Hooks.init!
  50. end
  51. end
  52. def self.load!
  53. hook!
  54. end
  55. private
  56. def self.rails?
  57. defined?(::Rails)
  58. end
  59. def self.sinatra?
  60. defined?(::Sinatra)
  61. end
  62. end
  63. Kaminari.load!