Module: RailsAgnosticModels::ActiveRecordExtensions::ClassMethods

Defined in:
lib/rails_agnostic_models/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#safe_constant(constant) ⇒ Object

safely refer to constants that may not be defined. Useful in a gem that might get included in places that might not define EVERY active record model, such as a satelite administration application. Note that you will still need to handle nil cases

safe_constant(:Object) # => Object safe_constant(:ClassThatDoesNotExist) # => nil safe_constant(“Object”) # => Object safe_constant(“MyModule::MyClass”) # => MyModule::MyClass



13
14
15
16
17
18
19
20
21
22
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 13

def safe_constant(constant)
  case constant
  when Symbol then
    return top_level_constant_lookup constant
  when String then
    drill_down_constant_lookup(constant)
  else
    return nil
  end
end

#version_agnostic_default_scope(options = {}) ⇒ Object

passes an options hash to rails 2, translates to an arel call in Rails 3



43
44
45
46
47
48
49
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 43

def version_agnostic_default_scope(options = {})
  if rails_2?
    default_scope options
  else
    self.instance_eval "default_scope #{ArelTranslator::Translator.new(options).translate!}"
  end
end

#version_agnostic_inheritance_column(column_name) ⇒ Object

Sets the inheritance column based on the version of Rails



34
35
36
37
38
39
40
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 34

def version_agnostic_inheritance_column(column_name)
  if rails_2?
    set_inheritance_column column_name
  else
    self.inheritance_column = column_name
  end
end

#version_agnostic_scope(*args) ⇒ Object

Defines the appropraite scope based on the version of Rails



25
26
27
28
29
30
31
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 25

def version_agnostic_scope(*args)
  if rails_2?
    named_scope(*args)
  else
    scope(*args)
  end
end