Class: Ahoy::Generators::ActiverecordGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/ahoy/activerecord_generator.rb

Instance Method Summary collapse

Instance Method Details

#adapterObject

use connection_db_config instead of connection.adapter so database connection isn’t needed



42
43
44
# File 'lib/generators/ahoy/activerecord_generator.rb', line 42

def adapter
  ActiveRecord::Base.connection_db_config.adapter.to_s
end

#copy_templatesObject



12
13
14
15
16
17
18
# File 'lib/generators/ahoy/activerecord_generator.rb', line 12

def copy_templates
  template "database_store_initializer.rb", "config/initializers/ahoy.rb"
  template "active_record_visit_model.rb", "app/models/ahoy/visit.rb"
  template "active_record_event_model.rb", "app/models/ahoy/event.rb"
  migration_template "active_record_migration.rb", "db/migrate/create_ahoy_visits_and_events.rb", migration_version: migration_version
  puts "\nAlmost set! Last, run:\n\n    rails db:migrate"
end

#foreign_key_typeObject



54
55
56
# File 'lib/generators/ahoy/activerecord_generator.rb', line 54

def foreign_key_type
  ", type: :#{key_type}" if key_type
end

#key_typeObject



58
59
60
# File 'lib/generators/ahoy/activerecord_generator.rb', line 58

def key_type
  Rails.configuration.generators.options.dig(:active_record, :primary_key_type)
end

#migration_versionObject



46
47
48
# File 'lib/generators/ahoy/activerecord_generator.rb', line 46

def migration_version
  "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
end

#primary_key_typeObject



50
51
52
# File 'lib/generators/ahoy/activerecord_generator.rb', line 50

def primary_key_type
  ", id: :#{key_type}" if key_type
end

#properties_typeObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/generators/ahoy/activerecord_generator.rb', line 20

def properties_type
  case adapter
  when /postg/i # postgres, postgis
    "jsonb"
  when /mysql|trilogy/i
    "json"
  else
    "text"
  end
end

#serialize_optionsObject



36
37
38
# File 'lib/generators/ahoy/activerecord_generator.rb', line 36

def serialize_options
  ActiveRecord::VERSION::STRING.to_f >= 7.1 ? "coder: JSON" : "JSON"
end

#serialize_properties?Boolean

requires database connection to check for MariaDB

Returns:

  • (Boolean)


32
33
34
# File 'lib/generators/ahoy/activerecord_generator.rb', line 32

def serialize_properties?
  properties_type == "text" || (properties_type == "json" && ActiveRecord::Base.connection.try(:mariadb?))
end