class Thecore::ThecorizePluginGenerator

Public Instance Methods

add_ability_file() click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 58
    def add_ability_file
      # do this just the first time
      say "Adding abilities file", :green
      abilities_file_name = "abilities_#{@name}_concern.rb"
      abilities_file_fullpath = File.join(@plugin_initializers_dir, abilities_file_name)
      initializer abilities_file_name do
"
require 'active_support/concern'

module #{@name.classify}AbilitiesConcern
  extend ActiveSupport::Concern
  included do
    def #{@name}_abilities user
      if user
        # if the user is logged in, it can do certain tasks regardless his role
        if user.admin?
          # if the user is an admin, it can do a lot of things, usually
        end

        if user.has_role? :role
          # a specific role, brings specific powers
        end
      end
    end
  end
end

# include the extension
TheCoreAbilities.send(:include, #{@name.classify}AbilitiesConcern)
"
      end unless File.exists?(abilities_file_fullpath)
    end
add_after_initialize_file() click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 92
    def add_after_initialize_file
      # do this just the first time
      say "Adding after_initialize file", :green
      after_initialize_file_name = "#{@name}_after_initialize.rb"
      after_initialize_file_fullpath = File.join(@plugin_initializers_dir, after_initialize_file_name)
      initializer after_initialize_file_name do
"
Rails.application.configure do
  config.after_initialize do
  end
end
"
      end unless File.exists?(after_initialize_file_fullpath)
    end
add_require_thecore() click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 49
    def add_require_thecore
      inject_into_file "lib/#{@name}.rb", before: "require \"#{@name}/engine\"" do
"
require 'thecore'
"
      end
    end
init_constants() click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 10
def init_constants
  say "Setting the variables", :green
  @plugin_path = @destination_stack.first.match(Regexp.new("^.*#{@name}"))[0]
  @parent_path = File.expand_path("..", @plugin_path)
  @plugin_parent_name = @parent_path.split(File::SEPARATOR).last
  @plugin_initializers_dir = File.join(@plugin_path, "config", "initializers")
  @plugin_models_dir = File.join(@plugin_path, "app", "models")
  @plugin_lib_file = File.join(@plugin_path, "lib", @name, "engine.rb")
  Dir.chdir @plugin_models_dir do
    # Getting all the models that are activerecords:
    @model_files = Dir.glob("*.rb").map do |model|
      file = File.join(@plugin_models_dir,model)
      model if is_applicationrecord?(file)
    end.compact
  end
end
manage_git() click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 107
def manage_git
  say "Manage Git", :green
  rails_command "g thecore:add_git #{@name}"
end
migrations_to_main_app() click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 28
    def migrations_to_main_app
      say "Checking if it's an engine"
      if is_engine?(@plugin_lib_file) && !has_add_to_migrations_declaration?(@plugin_lib_file)
        say "Adding migration reflection into engine.rb of", :green
        inject_into_file @plugin_lib_file, after: "class Engine < ::Rails::Engine\n" do
"
    initializer '#{@name}.add_to_migrations' do |app|
      unless app.root.to_s.match root.to_s
        # APPEND TO MAIN APP MIGRATIONS FROM THIS GEM
        config.paths['db/migrate'].expanded.each do |expanded_path|
          app.config.paths['db/migrate'] << expanded_path
        end
      end
    end

"
        end
      end
    end
thecoreize_the_models() click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 112
def thecoreize_the_models
  rails_command "g thecore:thecorize_models #{@name}"
end

Private Instance Methods

ask_question_multiple_choice(models, question = "Choose among one of these, please.") click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 118
def ask_question_multiple_choice models, question = "Choose among one of these, please."
  return [] if models.empty?
  # raccolgo tutte le risposte che non siano cancel
  # e ritorno l'array
  return_array = []
  while (answer ||= "") != "cancel"
    remaining_models = (models-return_array)
    break if remaining_models.empty?
    answer = ask question, :red, limited_to: remaining_models.push("cancel").uniq
    break if answer == "cancel"
    return_array.push answer
  end
  return return_array
end
has_add_to_migrations_declaration?(file) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 157
def has_add_to_migrations_declaration? file
  (File.readlines(file).grep(/^[ \t]*initializer '[a-zA-Z0-9]+\.add_to_migrations' do \|app\|/).size > 0) rescue false
end
has_has_many_association?(file, assoc) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 161
def has_has_many_association? file, assoc
  reg_def = "^[ \\t]+has_many[ \\t]+:#{assoc}"
  (File.readlines(file).grep(Regexp.new(reg_def)).size > 0) rescue false
end
has_polymorphic_has_many?(file, polymorphic_name) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 137
def has_polymorphic_has_many? file, polymorphic_name
  (File.readlines(file).grep(/^[ \t]*has_many.+as: :#{polymorphic_name}.*/).size > 0) rescue false
end
has_rails_admin_declaration?(file) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 149
def has_rails_admin_declaration? file
  (File.readlines(file).grep(/^[ \t]*rails_admin do/).size > 0) rescue false
end
is_activerecord?(file) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 141
def is_activerecord? file
  (File.readlines(file).grep(/^class [A-Za-z0-9]+ < ActiveRecord::Base/).size > 0) rescue false
end
is_applicationrecord?(file) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 145
def is_applicationrecord? file
  (File.readlines(file).grep(/^class [A-Za-z0-9]+ < ApplicationRecord/).size > 0) rescue false
end
is_engine?(file) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 153
def is_engine? file
  (File.readlines(file).grep(/^[ \t]*class Engine < ::Rails::Engine/).size > 0) rescue false
end
is_has_many_through?(file, assoc, through) click to toggle source
# File lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb, line 133
def is_has_many_through? file, assoc, through
  (File.readlines(file).grep(/^[ \t]*has_many[ \t]+:#{assoc},[ \t]+through:[ \t]+:#{through}.*/).size > 0) rescue false
end