Module: RailsAgnosticModels::ActiveRecordExtensions::ClassMethods

Defined in:
lib/rails_agnostic_models/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#rails(&block) ⇒ Object

Code to be executed only inside of a rails app



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

def rails(&block)
  return nil unless rails_loaded
  yield
end

#rails_2(&block) ⇒ Object

Takes a block that will only be executed in a Rails 2 Project



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

def rails_2(&block)
  return nil unless rails_2?
  yield
end

#rails_2?Boolean

Checks if the host app is a Rails 2 app

Returns:

  • (Boolean)


7
8
9
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 7

def rails_2?
  rails_loaded && (Rails::VERSION::MAJOR == 2)
end

#rails_3(&block) ⇒ Object

Code to be executed insode of a Rails 3 app



33
34
35
36
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 33

def rails_3(&block)
  return nil unless rails_3?
  yield
end

#rails_3?Boolean

Checks if the host app is a Rails 3 app

Returns:

  • (Boolean)


12
13
14
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 12

def rails_3?
  rails_loaded && (Rails::VERSION::MAJOR == 3)
end

#rails_4(&block) ⇒ Object

Code to be executed inside of a Rails 4 app



39
40
41
42
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 39

def rails_4(&block)
  return nil unless rails_4?
  yield
end

#rails_4?Boolean

Checks if the host app is a Rails 4 app

Returns:

  • (Boolean)


17
18
19
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 17

def rails_4?
  rails_loaded && (Rails::VERSION::MAJOR == 4)
end

#rails_loadedObject

Checks if Rails is loaded



22
23
24
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 22

def rails_loaded
  defined? Rails
end

#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



57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 57

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



87
88
89
90
91
92
93
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 87

def version_agnostic_default_scope(options = {})
  if rails_2?
    default_scope options
  else
    self.instance_eval "default_scope #{options_to_arel(options)}"
  end
end

#version_agnostic_inheritance_column(column_name) ⇒ Object

Sets the inheritance column based on the version of Rails



78
79
80
81
82
83
84
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 78

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



69
70
71
72
73
74
75
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 69

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