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



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

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



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

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)


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

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

#rails_3(&block) ⇒ Object

Code to be executed insode of a Rails 3 app



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

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)


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

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

#rails_4(&block) ⇒ Object

Code to be executed inside of a Rails 4 app



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

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)


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

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

#rails_loadedObject

Checks if Rails is loaded



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

def rails_loaded
  defined? Rails
end

#safe_constant(constant_sym) ⇒ 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



51
52
53
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 51

def safe_constant(constant_sym)
  return Object.const_get(constant_sym) if Object.const_defined? constant_sym
end

#version_agnostic_inheritance_column(column_name) ⇒ Object

Sets the inheritance column based on the version of Rails



65
66
67
68
69
70
71
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 65

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



56
57
58
59
60
61
62
# File 'lib/rails_agnostic_models/active_record_extensions.rb', line 56

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