Class: Dart::NamingConventions::AbstractBase

Inherits:
Object
  • Object
show all
Defined in:
lib/dart/naming_conventions/abstract_base.rb

Direct Known Subclasses

Reflection::Sequel::NamingConventions

Instance Method Summary collapse

Instance Method Details

#conventional_join_table_names(left_table_name, right_table_name) ⇒ Object

Returns unique singular and plural combinations of the given table names that would be used in constructing a conventional join table name (assumes the given table_names are plural)



61
62
63
64
65
66
67
# File 'lib/dart/naming_conventions/abstract_base.rb', line 61

def conventional_join_table_names(left_table_name, right_table_name)
  # TODO consider [left_table_name, pluralize(left_table_name), singularize(left_table_name)].uniq
  left_names  = [left_table_name, singularize(left_table_name)].uniq
  right_names = [right_table_name, singularize(right_table_name)].uniq

  (left_names.product(right_names) + right_names.product(left_names)).map { |t1, t2| "#{t1}_#{t2}" }
end

#long_association_name(join_table, left_key, left_table, right_key) ⇒ Object

Returns a long many_to_many association name based on the given join table and foreign_keys according to the naming convention Examples:

long_association_name('topic_assignments', 'group_id', 'groups', 'user_id') => 'topic_assignment_users'
long_association_name('broadcasts', 'created_by', 'users', 'item_id') => 'broadcast_created_by_items'


54
55
56
57
# File 'lib/dart/naming_conventions/abstract_base.rb', line 54

def long_association_name(join_table, left_key, left_table, right_key)
  left_key_qualifier = "_#{left_key}" if left_table != parent_table_for(left_key)
  "#{singularize(join_table)}#{left_key_qualifier}_#{plural_association_name(right_key)}"
end

#parent_table_for(possible_foreign_key) ⇒ String|NilClass

Returns the name of a possibly referenced table if the given possible_foreign_key follows the naming convention, otherwise returns nil

Examples:

parent_table_for('group_id') => 'groups'
parent_table_for('created_by') => nil

Parameters:

  • possible_foreign_key (String|Symbol)

    name of the possibly referencing column

Returns:

  • (String|NilClass)

    name of the possibly referenced table if found by convention



20
21
22
# File 'lib/dart/naming_conventions/abstract_base.rb', line 20

def parent_table_for(possible_foreign_key)
  pluralize singular_association_name(possible_foreign_key) if possible_foreign_key =~ foreign_key_regex
end

#plural_association_name(foreign_key) ⇒ Object

Returns a one_to_many association name based on the given foreign_key according to the naming convention Examples:

to_association('group_id') => 'groups'
to_association('team_id') => 'teams'
to_association('created_by') => 'created_bies'


44
45
46
# File 'lib/dart/naming_conventions/abstract_base.rb', line 44

def plural_association_name(foreign_key)
  pluralize singular_association_name(foreign_key)
end

#singular_association_name(foreign_key) ⇒ Object

Returns a many_to_one association name based on the given foreign_key according to the naming convention Examples:

to_association('group_id') => 'group'
to_association('team_id') => 'team'
to_association('created_by') => 'created_by'


30
31
32
33
34
35
36
# File 'lib/dart/naming_conventions/abstract_base.rb', line 30

def singular_association_name(foreign_key)
  if foreign_key =~ foreign_key_regex
    foreign_key[0...foreign_key.index(foreign_key_regex)]
  else
    foreign_key
  end
end