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.



1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1398

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.



1396
1397
1398
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1396

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



1396
1397
1398
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1396

def options
  @options
end

Instance Method Details

#autosave(autosave) ⇒ Object



1474
1475
1476
1477
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1474

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

#class_name(class_name) ⇒ Object



1484
1485
1486
1487
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1484

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

#conditions(conditions) ⇒ Object



1464
1465
1466
1467
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1464

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

#counter_cache(counter_cache = true) ⇒ Object



1437
1438
1439
1440
1441
1442
1443
1444
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1437

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

#dependent(dependent) ⇒ Object



1419
1420
1421
1422
1423
1424
1425
1426
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1419

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

#descriptionObject



1554
1555
1556
1557
1558
1559
1560
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1554

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



1562
1563
1564
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1562

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

#failure_message_when_negatedObject



1566
1567
1568
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1566

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

#index_errors(index_errors) ⇒ Object



1479
1480
1481
1482
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1479

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

#inverse_of(inverse_of) ⇒ Object



1446
1447
1448
1449
1450
1451
1452
1453
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1446

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

#join_table(join_table_name) ⇒ Object



1544
1545
1546
1547
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1544

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

#join_table_nameObject



1592
1593
1594
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1592

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1570

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

#option_verifierObject



1596
1597
1598
1599
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1596

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

#optional(optional = true) ⇒ Object



1519
1520
1521
1522
1523
1524
1525
1526
1527
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1519

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

#order(order) ⇒ Object



1428
1429
1430
1431
1432
1433
1434
1435
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1428

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

#required(required = true) ⇒ Object



1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1509

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

#source(source) ⇒ Object



1455
1456
1457
1458
1459
1460
1461
1462
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1455

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

#strict_loading(strict_loading = true) ⇒ Object



1539
1540
1541
1542
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1539

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

#through(through) ⇒ Object



1410
1411
1412
1413
1414
1415
1416
1417
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1410

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

#touch(touch = true) ⇒ Object



1534
1535
1536
1537
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1534

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

#types(types) ⇒ Object



1469
1470
1471
1472
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1469

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

#validate(validate = true) ⇒ Object



1529
1530
1531
1532
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1529

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

#with_foreign_key(foreign_key) ⇒ Object



1489
1490
1491
1492
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1489

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

#with_foreign_type(foreign_type) ⇒ Object



1494
1495
1496
1497
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1494

def with_foreign_type(foreign_type)
  @options[:foreign_type] = foreign_type
  self
end

#with_primary_key(primary_key) ⇒ Object



1499
1500
1501
1502
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1499

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

#with_query_constraints(query_constraints) ⇒ Object



1504
1505
1506
1507
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1504

def with_query_constraints(query_constraints)
  @options[:query_constraints] = query_constraints
  self
end

#without_validating_presenceObject



1549
1550
1551
1552
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 1549

def without_validating_presence
  remove_submatcher(AssociationMatchers::RequiredMatcher)
  self
end