Module: Consul::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/consul/util.rb

Instance Method Summary collapse

Instance Method Details

#adjective_and_argument(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/consul/util.rb', line 49

def adjective_and_argument(*args)
  if args.size > 1
    adjective = args[0]
    record = args[1]
  else
    adjective = nil
    record = args[0]
  end
  [adjective, record]
end

#around_action(controller_class, *args, &block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/consul/util.rb', line 82

def around_action(controller_class, *args, &block)
  if Rails.version.to_i < 4
    controller_class.around_filter *args, &block
  else
    controller_class.around_action *args, &block
  end
end

#before_action(controller_class, *args, &block) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/consul/util.rb', line 74

def before_action(controller_class, *args, &block)
  if Rails.version.to_i < 4
    controller_class.before_filter *args, &block
  else
    controller_class.before_action *args, &block
  end
end

#collection?(value) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/consul/util.rb', line 21

def collection?(value)
  value.is_a?(Array) || value.is_a?(Set)
end

#define_default_scope(klass, conditions) ⇒ Object

This method does not support dynamic default scopes via lambdas (as does #define_scope), because it is currently not required.



39
40
41
42
43
44
45
46
47
# File 'lib/consul/util.rb', line 39

def define_default_scope(klass, conditions)
  if Rails.version.to_i < 4 # Rails 2/3
    klass.send :default_scope, conditions
  else
    klass.send :default_scope do
      klass.scoped(conditions)
    end
  end
end

#define_scope(klass, name, lambda) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/consul/util.rb', line 25

def define_scope(klass, name, lambda)
  if Rails.version.to_i < 4 # Rails 2/3
    scope_definition = Rails.version.to_i < 3 ? :named_scope : :scope
    klass.send scope_definition, name, lambda
  else
    klass.send :scope, name, lambda { |*args|
      options = lambda.call(*args)
      klass.scoped(options.slice *EdgeRider::Scoped::VALID_FIND_OPTIONS)
    }
  end
end

#scope?(value) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/consul/util.rb', line 17

def scope?(value)
  value.respond_to?(:scoped)
end

#scope_selects_all_records?(scope) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/consul/util.rb', line 5

def scope_selects_all_records?(scope)
  if Rails.version.to_i < 3
    scope = scope.scoped({})
  else
    scope = scope.scoped
  end
  scope_sql = scope.to_sql
  quoted_table_name = Regexp.quote(scope.connection.quote_table_name(scope.table_name))
  all_sql_pattern = /\ASELECT (#{quoted_table_name}\.)?\* FROM #{quoted_table_name}\z/
  scope_sql.squish =~ all_sql_pattern
end

#skip_before_action(controller_class, name, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/consul/util.rb', line 60

def skip_before_action(controller_class, name, options)
  if Rails.version.to_i < 4
    controller_class.skip_before_filter name, options
  elsif Rails.version.to_i < 5
    controller_class.skip_before_action name, options
  else
    # Every `power` in a controller will skip the power check filter. After the 1st time, Rails 5+ will raise
    # an error because there is no `unchecked_power` action to skip any more.
    # To avoid this, we add the following extra option. Note that it must not be added in Rails 4 to avoid errors.
    # See http://api.rubyonrails.org/classes/ActiveSupport/Callbacks/ClassMethods.html#method-i-skip_callback
    controller_class.skip_before_action name, { :raise => false }.merge!(options)
  end
end