lib/active_admin/dependency.rb in activeadmin-1.0.0.pre2 vs lib/active_admin/dependency.rb in activeadmin-1.0.0.pre3

- old
+ new

@@ -1,9 +1,9 @@ module ActiveAdmin module Dependency module Requirements - DEVISE = '~> 3.2' + DEVISE = '>= 3.2', '< 5' end # Provides a clean interface to check for gem dependencies at runtime. # # ActiveAdmin::Dependency.draper @@ -53,10 +53,14 @@ def self.[](name) Matcher.new name.to_s end + def self.rails5? + rails >= '5.x' + end + class Matcher attr_reader :name def initialize(name) @name = name @@ -88,8 +92,77 @@ def inspect info = spec ? "#{spec.name} #{spec.version}" : '(missing)' "<ActiveAdmin::Dependency::Matcher for #{info}>" end + + def adapter + @adapter ||= Adapter.const_get(@name.camelize).new self + end + + def method_missing(method, *args, &block) + if respond_to_missing?(method) + adapter.send method, *args, &block + else + super + end + end + + def respond_to_missing?(method, include_private = false) + adapter.respond_to?(method) || super + rescue NameError + # 🐾 + end end + + # Dependency adapters provide an easy way to wrap the conditional logic + # necessary to support multiple versions of a gem. + # + # ActiveAdmin::Dependency.rails.adapter.parameterize 'a b' + # => 'a_b' + # + # ActiveAdmin::Dependency.rails.parameterize 'a b' + # => 'a_b' + # + # ActiveAdmin::Dependency.devise.adapter + # -> NameError: uninitialized constant ActiveAdmin::Dependency::Adapter::Devise + # + module Adapter + class Base + def initialize(version) + @version = version + end + end + + class Rails < Base + def strong_parameters? + @version >= 4 || defined?(ActionController::StrongParameters) + end + + def parameterize(string) + if Dependency.rails5? + string.parameterize separator: '_' + else + string.parameterize '_' + end + end + + def redirect_back(controller, fallback_location) + controller.instance_exec do + if Dependency.rails5? + redirect_back fallback_location: fallback_location + elsif controller.request.headers.key? 'HTTP_REFERER' + redirect_to :back + else + redirect_to fallback_location + end + end + end + + def render_key + Dependency.rails5? ? :body : :text + end + end + end + end end