Module: Fakey::TableDefinition
- Defined in:
- lib/fakey/schema_definitions.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_foreign_key(column_name, table_name, options) ⇒ Object
- #foreign_key(*args) ⇒ Object
- #references_with_foreign_keys(*args) ⇒ Object (also: #belongs_to_with_foreign_keys)
- #to_sql_with_foreign_keys ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/fakey/schema_definitions.rb', line 4 def self.included(base) base.class_eval do alias_method_chain :to_sql, :foreign_keys alias_method_chain :references, :foreign_keys alias_method_chain :belongs_to, :foreign_keys end end |
Instance Method Details
#add_foreign_key(column_name, table_name, options) ⇒ Object
29 30 31 32 |
# File 'lib/fakey/schema_definitions.rb', line 29 def add_foreign_key(column_name,table_name,) @foreign_keys ||= [] @foreign_keys << {:column_name => column_name, :to_table => table_name, :options => } end |
#foreign_key(*args) ⇒ Object
42 43 44 45 46 |
# File 'lib/fakey/schema_definitions.rb', line 42 def foreign_key(*args) return unless Array === args.first composite_foreign_key, = args.first, args.last end |
#references_with_foreign_keys(*args) ⇒ Object Also known as: belongs_to_with_foreign_keys
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fakey/schema_definitions.rb', line 12 def references_with_foreign_keys(*args) = args. polymorphic = .delete(:polymorphic) args.each do |col| column_name = [:column] || "#{col}_id" column_database_type = [:type] || :integer column(column_name, column_database_type, ) if polymorphic column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : ) else table_name = [:references] || col.to_s.pluralize add_foreign_key(column_name, table_name, ) end end end |
#to_sql_with_foreign_keys ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/fakey/schema_definitions.rb', line 34 def to_sql_with_foreign_keys sql = to_sql_without_foreign_keys if @foreign_keys sql << ',' << @foreign_keys.map{|fk| @base.add_foreign_key(fk[:to_table],fk[:column_name],fk[:options])}.join(",") end sql end |