Module: EdgeRider::Util
Instance Method Summary collapse
- #active_record_version ⇒ Object
- #define_association(owner, association, target, options) ⇒ Object
- #define_scope(klass, name, lambda) ⇒ Object
- #exclusive_query(model, conditions) ⇒ Object
- #qualify_column_name(model, column_name) ⇒ Object
- #rspec_path ⇒ Object
- #rspec_root ⇒ Object
- #rspec_version ⇒ Object
- #scope?(object) ⇒ Boolean
Instance Method Details
#active_record_version ⇒ Object
59 60 61 |
# File 'lib/edge_rider/util.rb', line 59 def active_record_version ActiveRecord::VERSION::MAJOR end |
#define_association(owner, association, target, options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/edge_rider/util.rb', line 46 def define_association(owner, association, target, ) if active_record_version < 4 owner.send association, target, else # Reduce the options hash to the given keys and store the remainder in # other_options. Using Hash#extract! would be easier to unterstand, # but Rails 2 does not have it. = .slice!(:conditions) scope = lambda { |*args| scoped() } owner.send association, target, scope, ** end end |
#define_scope(klass, name, lambda) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/edge_rider/util.rb', line 31 def define_scope(klass, name, lambda) if ActiveRecord::VERSION::MAJOR == 3 klass.send :scope, name, lambda else klass.send :scope, name, lambda { |*args| = lambda.call(*args) if ActiveRecord::VERSION::MAJOR < 6 klass.scoped(.slice :conditions) else scoped(.slice :conditions) end } end end |
#exclusive_query(model, conditions) ⇒ Object
23 24 25 |
# File 'lib/edge_rider/util.rb', line 23 def exclusive_query(model, conditions) model.unscoped.where(conditions) end |
#qualify_column_name(model, column_name) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/edge_rider/util.rb', line 7 def qualify_column_name(model, column_name) column_name = column_name.to_s unless column_name.include?('.') column_name = if ActiveRecord::VERSION::MAJOR < 4 quoted_table_name = model.connection.quote_table_name(model.table_name) quoted_column_name = model.connection.quote_column_name(column_name) "#{quoted_table_name}.#{quoted_column_name}" else # Rails 4+ will quote correctly and Rails 5.2 will print # deprecation warnings if there are surplus quotes "#{model.table_name}.#{column_name}" end end column_name end |
#rspec_path ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/edge_rider/util.rb', line 80 def rspec_path if rspec_version == 1 'spec' else 'rspec' end end |
#rspec_root ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/edge_rider/util.rb', line 72 def rspec_root if rspec_version == 1 Spec else RSpec end end |
#rspec_version ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/edge_rider/util.rb', line 63 def rspec_version if defined?(Spec) 1 else require 'rspec/version' RSpec::Version::STRING.to_i end end |
#scope?(object) ⇒ Boolean
27 28 29 |
# File 'lib/edge_rider/util.rb', line 27 def scope?(object) object.respond_to?(:scoped) end |