class Platforms::Core::Generators::Base
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
Public Instance Methods
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
# 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 a migration, when a model already exists. This could be for platforms_network_id or platforms_user_id.
# 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 the model, unless –existing_model is specified. Calls the standard rails model generator, so should accept similar arguments to 'rails g model Foo'.
# 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
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.
# 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