Module: BetterAuth::Hanami::Migration
- Defined in:
- lib/better_auth/hanami/migration.rb
Class Method Summary collapse
- .column_line(logical_field, attributes, options) ⇒ Object
- .create_table_lines(table, options) ⇒ Object
- .default_value(attributes) ⇒ Object
- .foreign_key_target(model, options) ⇒ Object
- .hanami_type(attributes) ⇒ Object
- .index_line(logical_field, attributes) ⇒ Object
- .physical_name(value) ⇒ Object
- .render(options) ⇒ Object
Class Method Details
.column_line(logical_field, attributes, options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/better_auth/hanami/migration.rb', line 39 def column_line(logical_field, attributes, ) column = attributes[:field_name] || physical_name(logical_field) reference = attributes[:references] if reference target = foreign_key_target(reference.fetch(:model), ) parts = ["foreign_key :#{column}, :#{target}", "type: #{hanami_type(attributes)}"] parts << "null: false" if attributes[:required] parts << "on_delete: :#{reference[:on_delete]}" if reference[:on_delete] return " #{parts.join(", ")}" end parts = ["column :#{column}", hanami_type(attributes)] parts << "null: false" if attributes[:required] default = default_value(attributes) parts << "default: #{default}" unless default.nil? " #{parts.join(", ")}" end |
.create_table_lines(table, options) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/better_auth/hanami/migration.rb', line 24 def create_table_lines(table, ) table_name = table.fetch(:model_name) lines = ["", " create_table :#{table_name} do"] table.fetch(:fields).each do |logical_field, attributes| lines << column_line(logical_field, attributes, ) end lines << " primary_key [:id]" if table.fetch(:fields).key?("id") table.fetch(:fields).each do |logical_field, attributes| index = index_line(logical_field, attributes) lines << index if index end lines << " end" lines end |
.default_value(attributes) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/better_auth/hanami/migration.rb', line 74 def default_value(attributes) default = attributes[:default_value] return if default.respond_to?(:call) case default when true then "true" when false then "false" when Numeric then default.to_s when String then default.inspect end end |
.foreign_key_target(model, options) ⇒ Object
86 87 88 89 |
# File 'lib/better_auth/hanami/migration.rb', line 86 def foreign_key_target(model, ) tables = BetterAuth::Schema.auth_tables() tables.fetch(model.to_s, nil)&.fetch(:model_name) || model end |
.hanami_type(attributes) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/better_auth/hanami/migration.rb', line 65 def hanami_type(attributes) case attributes[:type] when "boolean" then "TrueClass" when "date" then "DateTime" when "number" then "Integer" else "String" end end |
.index_line(logical_field, attributes) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/better_auth/hanami/migration.rb', line 57 def index_line(logical_field, attributes) return unless attributes[:unique] || attributes[:index] column = attributes[:field_name] || physical_name(logical_field) unique = attributes[:unique] ? ", unique: true" : "" " index :#{column}#{unique}" end |
.physical_name(value) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/better_auth/hanami/migration.rb', line 91 def physical_name(value) value.to_s .gsub(/([a-z\d])([A-Z])/, "\\1_\\2") .tr("-", "_") .downcase end |
.render(options) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/better_auth/hanami/migration.rb', line 8 def render() tables = BetterAuth::Schema.auth_tables() lines = [ "# frozen_string_literal: true", "", "require \"date\"", "require \"rom-sql\"", "", "ROM::SQL.migration do", " change do" ] tables.each_value { |table| lines.concat(create_table_lines(table, )) } lines.concat([" end", "end", ""]) lines.join("\n") end |