class Kriangle::Generators::ModuleGenerator
Custom scaffolding generator
Constants
- CONTROLLER_ACTIONS
Public Class Methods
new(*args, &block)
click to toggle source
Calls superclass method
# File lib/generators/kriangle/module_generator.rb, line 96 def initialize(*args, &block) super @controller_actions = [] @model_attributes = [] @model_associations = [] @attributes = [] @references = [] @polymorphics = [] @user_class = options.user_class&.underscore @wrapper = options.wrapper @database = Kriangle.database @controller_path = options.controller_path&.classify&.pluralize || controller_class_name @force = options.force @resources = options.resources? @reference = options.reference? if @reference @association_type = options.association_type @touch_record = options.touch_record? @accepts_nested_attributes = options.accepts_nested_attributes? @counter_cache = options.counter_cache? @reference_name = options.reference_name if @reference_name.match(/current_/) @reference_name_create_update = @reference_name @user_class ||= @reference_name.gsub(/current_/, '').underscore else @user_class ||= @reference_name.underscore @reference_id_param = get_attribute_name(@reference_name.underscore, 'references') @reference_name_create_update = "#{@reference_name}.find(params[:#{reference_id_param}])" @reference_name = "#{@reference_name}.find(params[:#{reference_id_param}])" end @model_associations << Association.new('belongs_to', user_class, nil, 'true', counter_cache.to_s, touch_record.to_s, accepts_nested_attributes.to_s, '', '', true) end @self_reference = options.self_reference? if @self_reference @parent_association_name = options.parent_association_name @child_association_name = options.child_association_name @additional_where_clause = @self_reference ? '.only_parent' : '' end @creation_method = options.creation_method @like_dislike = options.like_dislike @custom_orm = 'ActiveRecord' # Kriangle.custom_orm @initial_setup = options.initial_setup? @skip_tips = options.skip_tips? @skip_swagger = options.skip_swagger? @skip_model = options.skip_model? @skip_controller = options.skip_controller? @skip_migration = options.skip_migration? @skip_serializer = options.skip_serializer? @skip_timestamps = options.skip_timestamps? @skip_pagination = options.skip_pagination? # skip authentication if authenticator not found @skip_authentication = options.skip_authentication? @skip_authentication = true unless File.exist?(File.join(destination_root, 'app/controllers/api/authenticator.rb')) @description_method_name = @skip_authentication ? 'desc' : 'description' args_for_c_m.each do |arg| if arg.include?(':') || !CONTROLLER_ACTIONS.include?(arg) options = arg.split(':') if arg.match(/^ma:/).present? options.shift @model_associations << Association.new(*options) else @model_attributes << Attribute.new(*options) end else @controller_actions << arg @controller_actions << 'create' if arg == 'new' @controller_actions << 'update' if arg == 'edit' end end # Default controller actions if @controller_actions.blank? @controller_actions = %w[show create update destroy] @controller_actions << 'index' if @resources end # Get attribute's name @search_by = model_attributes.any? { |ma| ma.search_by.present? } # get different types of attributes @model_attributes.uniq!(&:name) @model_attributes.each do |attribute| if attribute.type.match('polymorphic').present? @polymorphics << attribute elsif attribute.type.match('references').present? @references << attribute else @attributes << attribute end end end
next_migration_number(_path)
click to toggle source
# File lib/generators/kriangle/module_generator.rb, line 195 def self.next_migration_number(_path) if @prev_migration_nr @prev_migration_nr += 1 else @prev_migration_nr = Time.now.utc.strftime('%Y%m%d%H%M%S').to_i end @prev_migration_nr.to_s end
Public Instance Methods
copy_controller_and_spec_files()
click to toggle source
# File lib/generators/kriangle/module_generator.rb, line 266 def copy_controller_and_spec_files template 'controller.rb', "app/controllers/api/#{@wrapper.underscore}/#{controller_path.underscore}.rb" unless skip_controller inject_into_file "app/controllers/api/#{@wrapper.underscore}/controllers.rb", "\n\t\t\tmount Api::#{@wrapper.capitalize}::#{controller_path}", after: /Grape::API.*/ unless skip_controller end
copy_initializer()
click to toggle source
# File lib/generators/kriangle/module_generator.rb, line 204 def copy_initializer create_template 'application_record.rb', 'app/models/application_record.rb', skip_if_exist: true create_template 'swagger.rb', 'config/initializers/swagger.rb', skip_if_exist: true unless skip_swagger create_template 'base.rb', 'app/controllers/api/base.rb', skip_if_exist: true create_template 'custom_description.rb', 'app/controllers/api/custom_description.rb', skip_if_exist: true create_template 'responder.rb', 'app/controllers/api/responder.rb' create_template 'controllers.rb', "app/controllers/api/#{@wrapper.underscore}/controllers.rb", skip_if_exist: true unless skip_controller create_template 'defaults.rb', "app/controllers/api/#{@wrapper.underscore}/defaults.rb", skip_if_exist: true inject_into_file 'app/controllers/api/base.rb', "\n\t\t\tmount Api::#{wrapper.capitalize}::Controllers", after: /Grape::API.*/ if initial_setup inject_into_file 'config/routes.rb', "\n\tmount GrapeSwaggerRails::Engine => '/swagger'", after: /routes.draw.*/ unless skip_swagger inject_into_file 'config/routes.rb', "\n\tmount Api::Base, at: '/'", after: /routes.draw.*/ end end
create_model_file()
click to toggle source
# File lib/generators/kriangle/module_generator.rb, line 224 def create_model_file # default options options = { references: @references.map(&:name), polymorphics: @polymorphics.map(&:name) } # create module model & migration unless skip_model options[:attributes] = @attributes.select { |a| a.validate_presence == 'true' }.map(&:name) create_template 'model.rb', "app/models/#{singular_name}.rb", options end unless skip_model model_associations.select { |ma| ma.association_type == 'belongs_to' }.each do |ma| regex = "has_many :#{plural_name}" file_path = ma.class_name.present? ? "app/models/#{ma.class_name.underscore}.rb" : "app/models/#{ma.association_name}.rb" contents = File.foreach(file_path).grep /#{regex}/ next if contents.count != 0 association = "\n\thas_many :#{plural_name}, dependent: :destroy" association += "\n\taccepts_nested_attributes_for :#{plural_name}, allow_destroy: true" if ma.accepts_nested_attributes == 'true' belongs_to = ma.class_name.present? ? ma.class_name : ma.association_name.classify inject_into_file file_path, association, after: /class #{belongs_to} < ApplicationRecord.*/ end end create_migration_file 'module_migration.rb', "db/migrate/create_#{plural_name}.rb", force: force if !skip_migration && custom_orm == 'ActiveRecord' if custom_orm == 'ActiveRecord' model_associations.select { |ma| ma.association_type == 'belongs_to' && ma.counter_cache == 'true' }.uniq { |ma| ma.association_name.classify && ma.class_name }.each do |ma| belongs_to = ma.class_name.present? ? ma.class_name.underscore : ma.association_name create_migration_file 'counter_cache_migration.rb', "db/migrate/add_#{class_name.pluralize.underscore}_count_to_#{belongs_to.pluralize}.rb", force: force, belongs_to: belongs_to end end # create active serializer & module serializer unless skip_serializer create_template 'active_serializer.rb', 'app/serializers/active_serializer.rb', skip_if_exist: true options[:attributes] = [:id] + @attributes.map(&:name) create_template 'serializer.rb', "app/serializers/#{singular_name}_serializer.rb", options inject_into_file "app/serializers/#{user_class}_serializer.rb", "\n\tattributes :#{class_name.pluralize.underscore}_count", after: /class #{user_class.classify}Serializer < ActiveSerializer*/ if counter_cache && custom_orm == 'ActiveRecord' end end