Sha256: b7583b82d13524137d5bbdaeb9f117696f370ac6e449431aaafdad87f1f9b8be
Contents?: true
Size: 1.94 KB
Versions: 2
Compression:
Stored size: 1.94 KB
Contents
# frozen-string-literal: true require "rails/generators/active_record/migration/migration_generator" module Mobility module BackendGenerators class Base < ::Rails::Generators::NamedBase argument :attributes, type: :array, default: [] include ::ActiveRecord::Generators::Migration include ::Mobility::ActiveRecordMigrationCompatibility def create_migration_file if self.class.migration_exists?(migration_dir, migration_file) ::Kernel.warn "Migration already exists: #{migration_file}" else migration_template "#{template}.rb", "db/migrate/#{migration_file}.rb" end end def self.next_migration_number(dirname) ::ActiveRecord::Generators::Base.next_migration_number(dirname) end def backend self.class.name.split('::').last.gsub(/Backend$/,'').underscore end protected def attributes_with_index attributes.select { |a| !a.reference? && a.has_index? } end private def check_data_source! unless data_source_exists? raise NoTableDefined, "The table #{table_name} does not exist. Create it first before generating translated columns." end end def data_source_exists? connection.data_source_exists?(table_name) end delegate :connection, to: ::ActiveRecord::Base def truncate_index_name(index_name) if index_name.size < connection.index_name_length index_name else "index_#{Digest::SHA1.hexdigest(index_name)}"[0, connection.index_name_length].freeze end end def template "#{backend}_translations" end def migration_dir File.expand_path("db/migrate") end def migration_file "create_#{file_name}_#{attributes.map(&:name).join('_and_')}_translations_for_mobility_#{backend}_backend" end end class NoTableDefined < StandardError; end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mobility-0.5.1 | lib/rails/generators/mobility/backend_generators/base.rb |
mobility-0.5.0 | lib/rails/generators/mobility/backend_generators/base.rb |