disable_prefix_check.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. module DisablePrefixCheck
  2. extend ActiveSupport::Concern
  3. module ClassMethods
  4. def check_prefix_options(options)
  5. end
  6. # `flexible = true` is hack to allow multiple things through the same AR class
  7. def conditional_prefix(resource, flexible = false)
  8. resource_id = "#{resource}_id".to_sym
  9. resource_type = flexible ? ":#{resource}" : resource.to_s.pluralize
  10. init_prefix_explicit resource_type, resource_id
  11. define_singleton_method :prefix do |options = {}|
  12. resource_type = options[resource] if flexible
  13. options[resource_id].nil? ? "/admin/" : "/admin/#{resource_type}/#{options[resource_id]}/"
  14. end
  15. define_singleton_method :instantiate_record do |record, prefix_options = {}|
  16. new_record(record).tap do |resource|
  17. resource.prefix_options = prefix_options unless prefix_options.blank?
  18. end
  19. end
  20. end
  21. def new_record(record)
  22. if ActiveSupport::VERSION::MAJOR == 3 && ActiveSupport::VERSION::MINOR == 0
  23. new(record)
  24. else
  25. new(record, true)
  26. end
  27. end
  28. end
  29. end