Class: Ahoy::Generators::ActiverecordGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Ahoy::Generators::ActiverecordGenerator
- Includes:
- ActiveRecord::Generators::Migration
- Defined in:
- lib/generators/ahoy/activerecord_generator.rb
Instance Method Summary collapse
-
#adapter ⇒ Object
use connection_db_config instead of connection.adapter so database connection isn’t needed.
- #copy_templates ⇒ Object
- #foreign_key_type ⇒ Object
- #key_type ⇒ Object
- #migration_version ⇒ Object
- #primary_key_type ⇒ Object
- #properties_type ⇒ Object
- #serialize_options ⇒ Object
-
#serialize_properties? ⇒ Boolean
requires database connection to check for MariaDB.
Instance Method Details
#adapter ⇒ Object
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_templates ⇒ Object
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_type ⇒ Object
54 55 56 |
# File 'lib/generators/ahoy/activerecord_generator.rb', line 54 def foreign_key_type ", type: :#{key_type}" if key_type end |
#key_type ⇒ Object
58 59 60 |
# File 'lib/generators/ahoy/activerecord_generator.rb', line 58 def key_type Rails.configuration.generators..dig(:active_record, :primary_key_type) end |
#migration_version ⇒ Object
46 47 48 |
# File 'lib/generators/ahoy/activerecord_generator.rb', line 46 def migration_version "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]" end |
#primary_key_type ⇒ Object
50 51 52 |
# File 'lib/generators/ahoy/activerecord_generator.rb', line 50 def primary_key_type ", id: :#{key_type}" if key_type end |
#properties_type ⇒ Object
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_options ⇒ Object
36 37 38 |
# File 'lib/generators/ahoy/activerecord_generator.rb', line 36 def ActiveRecord::VERSION::STRING.to_f >= 7.1 ? "coder: JSON" : "JSON" end |
#serialize_properties? ⇒ Boolean
requires database connection to check for MariaDB
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 |