extend.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. module RailsSettings
  2. module Extend
  3. extend ActiveSupport::Concern
  4. included do
  5. scope :with_settings, lambda {
  6. joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
  7. settings.thing_type = '#{base_class.name}')")
  8. .select("DISTINCT #{table_name}.*")
  9. }
  10. scope :with_settings_for, lambda { |var|
  11. joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
  12. settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
  13. }
  14. scope :without_settings, lambda {
  15. joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
  16. settings.thing_type = '#{base_class.name}')")
  17. .where("settings.id IS NULL")
  18. }
  19. scope :without_settings_for, lambda { |var|
  20. where("settings.id IS NULL")
  21. .joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
  22. settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
  23. }
  24. end
  25. def settings
  26. ScopedSettings.for_thing(self)
  27. end
  28. end
  29. end