Module: OrderCop

Defined in:
lib/order_cop.rb,
lib/order_cop/version.rb,
lib/order_cop/install_generator.rb

Defined Under Namespace

Modules: OrderCopMixin Classes: Error, InstallGenerator, OrderCopConfig, OrderCopRailtie

Constant Summary collapse

VERSION =
"0.2.3"

Class Method Summary collapse

Class Method Details

.apply(app = Rails.application) ⇒ Object



183
184
185
# File 'lib/order_cop.rb', line 183

def self.apply(app = Rails.application)
  patch_active_record(app) if enabled?
end

.config(**options) {|@config| ... } ⇒ Object

Yields:



35
36
37
38
39
40
41
42
# File 'lib/order_cop.rb', line 35

def self.config(**options, &block)
  @config ||= OrderCopConfig.new
  options.each do |key, value|
    @config.send("#{key}=", value)
  end
  yield @config if block
  @config
end

.disable(&block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/order_cop.rb', line 44

def self.disable(&block)
  old_enabled = @config.enabled
  @config.enabled = false
  yield
ensure
  @config.enabled = old_enabled
end

.disabled?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/order_cop.rb', line 52

def self.disabled?
  !config.enabled
end

.enabled?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/order_cop.rb', line 56

def self.enabled?
  config.enabled
end

.patch_active_record(app) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/order_cop.rb', line 169

def self.patch_active_record(app)
  ActiveRecord::Base.descendants.each do |model|
    model.const_get(:ActiveRecord_Associations_CollectionProxy).class_eval do
      prepend OrderCopMixin
    end
    model.const_get(:ActiveRecord_Relation).class_eval do
      prepend OrderCopMixin
    end
    model.const_get(:ActiveRecord_AssociationRelation).class_eval do
      prepend OrderCopMixin
    end
  end
end