Class: Shoulda::Matchers::ActiveRecord::AssociationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/association_matcher.rb

Constant Summary collapse

MACROS =
{
  'belongs_to' => 'belong to',
  'has_many' => 'have many',
  'has_one' => 'have one',
  'has_and_belongs_to_many' => 'have and belong to many',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(macro, name) ⇒ AssociationMatcher

Returns a new instance of AssociationMatcher.



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1000

def initialize(macro, name)
  @macro = macro
  @name = name
  @options = {}
  @submatchers = []
  @missing = ''

  if macro == :belongs_to
    required(belongs_to_required_by_default?)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



998
999
1000
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 998

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



998
999
1000
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 998

def options
  @options
end

Instance Method Details

#autosave(autosave) ⇒ Object



1071
1072
1073
1074
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1071

def autosave(autosave)
  @options[:autosave] = autosave
  self
end

#class_name(class_name) ⇒ Object



1081
1082
1083
1084
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1081

def class_name(class_name)
  @options[:class_name] = class_name
  self
end

#conditions(conditions) ⇒ Object



1066
1067
1068
1069
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1066

def conditions(conditions)
  @options[:conditions] = conditions
  self
end

#counter_cache(counter_cache = true) ⇒ Object



1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1039

def counter_cache(counter_cache = true)
  add_submatcher(
    AssociationMatchers::CounterCacheMatcher,
    counter_cache,
    name,
  )
  self
end

#dependent(dependent) ⇒ Object



1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1021

def dependent(dependent)
  add_submatcher(
    AssociationMatchers::DependentMatcher,
    dependent,
    name,
  )
  self
end

#descriptionObject



1136
1137
1138
1139
1140
1141
1142
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1136

def description
  description = "#{macro_description} #{name}"
  if options.key?(:class_name)
    description += " class_name => #{options[:class_name]}"
  end
  [description, submatchers.map(&:description)].flatten.join(' ')
end

#failure_messageObject



1144
1145
1146
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1144

def failure_message
  "Expected #{expectation} (#{missing_options})"
end

#failure_message_when_negatedObject



1148
1149
1150
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1148

def failure_message_when_negated
  "Did not expect #{expectation}"
end

#index_errors(index_errors) ⇒ Object



1076
1077
1078
1079
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1076

def index_errors(index_errors)
  @options[:index_errors] = index_errors
  self
end

#inverse_of(inverse_of) ⇒ Object



1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1048

def inverse_of(inverse_of)
  add_submatcher(
    AssociationMatchers::InverseOfMatcher,
    inverse_of,
    name,
  )
  self
end

#join_table(join_table_name) ⇒ Object



1126
1127
1128
1129
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1126

def join_table(join_table_name)
  @options[:join_table_name] = join_table_name
  self
end

#join_table_nameObject



1170
1171
1172
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1170

def join_table_name
  options[:join_table_name] || reflector.join_table_name
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1152

def matches?(subject)
  @subject = subject
  association_exists? &&
    macro_correct? &&
    validate_inverse_of_through_association &&
    (polymorphic? || class_exists?) &&
    foreign_key_exists? &&
    primary_key_exists? &&
    class_name_correct? &&
    join_table_correct? &&
    autosave_correct? &&
    index_errors_correct? &&
    conditions_correct? &&
    validate_correct? &&
    touch_correct? &&
    submatchers_match?
end

#option_verifierObject



1174
1175
1176
1177
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1174

def option_verifier
  @_option_verifier ||=
    AssociationMatchers::OptionVerifier.new(reflector)
end

#optional(optional = true) ⇒ Object



1106
1107
1108
1109
1110
1111
1112
1113
1114
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1106

def optional(optional = true)
  remove_submatcher(AssociationMatchers::RequiredMatcher)
  add_submatcher(
    AssociationMatchers::OptionalMatcher,
    name,
    optional,
  )
  self
end

#order(order) ⇒ Object



1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1030

def order(order)
  add_submatcher(
    AssociationMatchers::OrderMatcher,
    order,
    name,
  )
  self
end

#required(required = true) ⇒ Object



1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1096

def required(required = true)
  remove_submatcher(AssociationMatchers::OptionalMatcher)
  add_submatcher(
    AssociationMatchers::RequiredMatcher,
    name,
    required,
  )
  self
end

#source(source) ⇒ Object



1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1057

def source(source)
  add_submatcher(
    AssociationMatchers::SourceMatcher,
    source,
    name,
  )
  self
end

#through(through) ⇒ Object



1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1012

def through(through)
  add_submatcher(
    AssociationMatchers::ThroughMatcher,
    through,
    name,
  )
  self
end

#touch(touch = true) ⇒ Object



1121
1122
1123
1124
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1121

def touch(touch = true)
  @options[:touch] = touch
  self
end

#validate(validate = true) ⇒ Object



1116
1117
1118
1119
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1116

def validate(validate = true)
  @options[:validate] = validate
  self
end

#with_foreign_key(foreign_key) ⇒ Object



1086
1087
1088
1089
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1086

def with_foreign_key(foreign_key)
  @options[:foreign_key] = foreign_key
  self
end

#with_primary_key(primary_key) ⇒ Object



1091
1092
1093
1094
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1091

def with_primary_key(primary_key)
  @options[:primary_key] = primary_key
  self
end

#without_validating_presenceObject



1131
1132
1133
1134
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1131

def without_validating_presence
  remove_submatcher(AssociationMatchers::RequiredMatcher)
  self
end