Class: Platforms::Core::Generators::Base

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/platforms/core/base.rb

Overview

These are common functions that apply to both User and Network model types. The setup for both is really quite similar:

  • Generating a model, or creating a migration to add an association column

  • Add the relevant Concern to the Model

  • Update the initializer to reference the new class

Direct Known Subclasses

NetworkGenerator, UserGenerator

Instance Method Summary collapse

Instance Method Details

#add_concern_to_modelObject

Adds the relevant concern to the network or user model. This is either Platforms::Core:AppNetwork or Platforms::Core::AppUser #file_name is inherited from Rails::Generators::NamedBase, according to guides.rubyonrails.org/generators.html



70
71
72
73
74
75
76
77
# File 'lib/generators/platforms/core/base.rb', line 70

def add_concern_to_model

  # Add the concern line
  model_file_path = File.join("app", "models", class_path, "#{file_name}.rb")
  inject_into_class model_file_path, class_name do
    "  include Platforms::Core::App#{concern_type.capitalize}\n\n"
  end
end

#create_migrationObject

Create a migration, when a model already exists. This could be for platforms_network_id or platforms_user_id.



59
60
61
62
63
64
# File 'lib/generators/platforms/core/base.rb', line 59

def create_migration
  return unless options[:existing_model]
  migration_name = table_name.classify.pluralize
  args = "AddPlatformsNetworkIdTo#{migration_name} platforms_#{concern_type}_id:integer"
  generate "migration", args
end

#create_modelObject

Create the model, unless –existing_model is specified. Calls the standard rails model generator, so should accept similar arguments to ‘rails g model Foo’.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/platforms/core/base.rb', line 36

def create_model
  return if options[:existing_model]
  args = [name]
  args << "platforms_#{concern_type}_id:integer"

  # Recreate arguments
  attributes.each do |a|
    args << "#{a.name}:#{a.type}#{a.has_index? ? ":index" : "" }"
  end

  # Recreate options
  options.each do |k, v|
    unless k == "existing_model"
      args << "--#{k}=#{v}"
    end
  end

  # Use the standard model generator
  generate "model", args.join(" ")
end

#edit_initializerObject

Set a line in the initializer to ‘config.network_class = “Network”’ or similar. This could also be ‘config.user_class = “MyUser”’

Rather than gsub, matching config lines are removed and then added again.



83
84
85
86
87
88
89
90
91
# File 'lib/generators/platforms/core/base.rb', line 83

def edit_initializer
  init = "config/initializers/platforms_core.rb"
  gsub_file init, /\s*config.#{concern_type}_class\s+= .*\n/, "\n"

  inject_into_file init, after: "Platforms::Core.configure do |config|\n" do
    "  config.#{concern_type}_class = \"#{class_name}\"\n"
  end

end