Module: Shoulda::Matchers::RailsShim

Defined in:
lib/shoulda/matchers/rails_shim.rb

Defined Under Namespace

Classes: FakeAttributeType

Class Method Summary collapse

Class Method Details

.action_pack_versionObject



6
7
8
9
10
# File 'lib/shoulda/matchers/rails_shim.rb', line 6

def action_pack_version
  Gem::Version.new(::ActionPack::VERSION::STRING)
rescue NameError
  Gem::Version.new('0')
end

.active_model_lt_7?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/shoulda/matchers/rails_shim.rb', line 28

def active_model_lt_7?
  Gem::Requirement.new('< 7').satisfied_by?(active_model_version)
end

.active_model_st_6_1?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/shoulda/matchers/rails_shim.rb', line 24

def active_model_st_6_1?
  Gem::Requirement.new('< 6.1').satisfied_by?(active_model_version)
end

.active_model_versionObject



18
19
20
21
22
# File 'lib/shoulda/matchers/rails_shim.rb', line 18

def active_model_version
  Gem::Version.new(::ActiveModel::VERSION::STRING)
rescue NameError
  Gem::Version.new('0')
end

.active_record_versionObject



12
13
14
15
16
# File 'lib/shoulda/matchers/rails_shim.rb', line 12

def active_record_version
  Gem::Version.new(::ActiveRecord::VERSION::STRING)
rescue NameError
  Gem::Version.new('0')
end

.attribute_serialization_coder_for(model, attribute_name) ⇒ Object



72
73
74
# File 'lib/shoulda/matchers/rails_shim.rb', line 72

def attribute_serialization_coder_for(model, attribute_name)
  serialized_attributes_for(model)[attribute_name.to_s]
end

.attribute_type_for(model, attribute_name) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/shoulda/matchers/rails_shim.rb', line 131

def attribute_type_for(model, attribute_name)
  attribute_types_for(model)[attribute_name.to_s]
rescue NotImplementedError
  if model.respond_to?(:type_for_attribute)
    model.type_for_attribute(attribute_name)
  else
    FakeAttributeType.new(model, attribute_name)
  end
end

.attribute_types_for(model) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/shoulda/matchers/rails_shim.rb', line 117

def attribute_types_for(model)
  if model.respond_to?(:attribute_types)
    model.attribute_types
  elsif model.respond_to?(:type_for_attribute)
    model.columns.inject({}) do |hash, column|
      key = column.name.to_s
      value = model.type_for_attribute(column.name)
      hash.merge(key => value)
    end
  else
    raise NotImplementedError
  end
end

.digestible_attributes_in(record) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/shoulda/matchers/rails_shim.rb', line 97

def digestible_attributes_in(record)
  record.methods.inject([]) do |array, method_name|
    match = method_name.to_s.match(
      /\A(\w+)_(?:confirmation|digest)=\Z/,
    )

    if match
      array.concat([match[1].to_sym])
    else
      array
    end
  end
end

.generate_validation_message(record, attribute, type, model_name, options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shoulda/matchers/rails_shim.rb', line 32

def generate_validation_message(
  record,
  attribute,
  type,
  model_name,
  options
)
  if record && record.errors.respond_to?(:generate_message)
    record.errors.generate_message(attribute.to_sym, type, options)
  else
    simply_generate_validation_message(
      attribute,
      type,
      model_name,
      options,
    )
  end
rescue RangeError
  simply_generate_validation_message(
    attribute,
    type,
    model_name,
    options,
  )
end

.has_secure_password?(record, attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/shoulda/matchers/rails_shim.rb', line 88

def has_secure_password?(record, attribute_name)
  if secure_password_module
    attribute_name == :password &&
      record.class.ancestors.include?(secure_password_module)
  else
    record.respond_to?("authenticate_#{attribute_name}")
  end
end

.parent_of(mod) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/shoulda/matchers/rails_shim.rb', line 80

def parent_of(mod)
  if mod.respond_to?(:module_parent)
    mod.module_parent
  else
    mod.parent
  end
end

.secure_password_moduleObject



111
112
113
114
115
# File 'lib/shoulda/matchers/rails_shim.rb', line 111

def secure_password_module
  ::ActiveModel::SecurePassword::InstanceMethodsOnActivation
rescue NameError
  nil
end

.serialized_attributes_for(model) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shoulda/matchers/rails_shim.rb', line 58

def serialized_attributes_for(model)
  type_serialized_defined = Object.const_defined?('ActiveRecord::Type::Serialized')
  attribute_types_for(model).
    inject({}) do |hash, (attribute_name, attribute_type)|
      if type_serialized_defined && attribute_type.is_a?(::ActiveRecord::Type::Serialized)
        hash.merge(attribute_name => attribute_type.coder)
      else
        hash
      end
    end
rescue NotImplementedError
  {}
end

.supports_full_attributes_api?(model) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/shoulda/matchers/rails_shim.rb', line 141

def supports_full_attributes_api?(model)
  defined?(::ActiveModel::Attributes) &&
    model.respond_to?(:attribute_types)
end

.validates_column_options?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/shoulda/matchers/rails_shim.rb', line 146

def validates_column_options?
  Gem::Requirement.new('>= 7.1.0').satisfied_by?(active_record_version)
end

.verb_for_updateObject



76
77
78
# File 'lib/shoulda/matchers/rails_shim.rb', line 76

def verb_for_update
  :patch
end