Class: Thecore::ThecorizePluginGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_ability_fileObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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_fileObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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_thecoreObject



49
50
51
52
53
54
55
# 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_constantsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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_gitObject



107
108
109
110
# 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_appObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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 == 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_modelsObject



112
113
114
# File 'lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb', line 112

def thecoreize_the_models
  rails_command "g thecore:thecorize_models #{@name}"
end