class Object

Public Instance Methods

create_admin_controller(type, temp_path) click to toggle source
# File lib/generators/controller.rb, line 1
def create_admin_controller(type, temp_path)
  # find the legal controller
  controller_path = "app/controllers/admin/legals_controller.rb"
  template_controller_path = "#{temp_path}/#{type}/admin_legals_controller.txt"

  new_controller_text = File.open(template_controller_path).read
  File.write(controller_path, new_controller_text)

  puts "++ Created -> #{controller_path}"
end
create_admin_views(type, temp_path) click to toggle source
# File lib/generators/views.rb, line 1
def create_admin_views(type, temp_path)
  admin_view_dir_path = 'app/views/admin/legals/'

  # create directory for legal views in admin
  Dir.mkdir admin_view_dir_path

  # find all template views
  template_views_path = "#{temp_path}/#{type}/admin_views/"
  Dir.entries(template_views_path).each do |template|
    next if template == "." or template == ".."

    new_text = File.open("#{template_views_path}#{template}").read
    new_file_name = "#{admin_view_dir_path}#{template.split(".")[0]}.html.erb"
    File.write(new_file_name, new_text)

    puts "++ Created -> #{new_file_name}"
  end
end
create_controller(type, temp_path) click to toggle source
# File lib/generators/controller.rb, line 12
def create_controller(type, temp_path)
  # find the legal controller
  controller_path = "app/controllers/legals_controller.rb"
  template_controller_path = "#{temp_path}/#{type}/legals_controller.txt"

  new_controller_text = File.open(template_controller_path).read
  File.write(controller_path, new_controller_text)

  puts "++ Created -> #{controller_path}"
end
create_views(type, temp_path) click to toggle source
# File lib/generators/views.rb, line 20
def create_views(type, temp_path)
  view_dir_path = 'app/views/legals/'

  # create directory for legal views
  Dir.mkdir view_dir_path

  # find all template views
  template_views_path = "#{temp_path}/#{type}/views/"
  Dir.entries(template_views_path).each do |template|
    next if template == "." or template == ".."

    new_text = File.open("#{template_views_path}#{template}").read
    new_file_name = "#{view_dir_path}#{template.split(".")[0]}.html.erb"
    File.write(new_file_name, new_text)

    puts "++ Created -> #{new_file_name}"
  end
end
overwrite_migration() click to toggle source
# File lib/generators/migration.rb, line 1
def overwrite_migration
  # find the legal migration
  migrations_path = "db/migrate/"
  migration_path = "#{migrations_path}#{Dir.entries(migrations_path).grep(/_create_legals.rb$/).first}"

  new_migration_text = ""
  migration = File.open(migration_path).read
  migration.each_line do |line|
    # add detault: true to migration
    if line.include?("t.boolean :is_visible")
      new_migration_text += "      t.boolean :is_visible, default: true \n"
    else
      new_migration_text += line
    end
  end

  # rewrite the migration file
  File.write(migration_path, new_migration_text)

  puts "++ Changed -> #{migration_path}"
end
overwrite_model(type, temp_path) click to toggle source
# File lib/generators/model.rb, line 1
def overwrite_model(type, temp_path)
  # find the legal model
  model_path = "app/models/legal.rb"
  template_model_path = "#{temp_path}/#{type}/legal.txt"

  new_model_text = File.open(template_model_path).read
  File.write(model_path, new_model_text)

  puts "++ Changed -> #{model_path}"

  if type == "multilingual"
    model_translation_path = "app/models/legal_translation.rb"
    template_model_translation_path = "#{temp_path}/#{type}/legal_translation.txt"

    new_model_translation_text = File.open(template_model_translation_path).read
    File.write(model_translation_path, new_model_translation_text)

    puts "++ Changed -> #{model_translation_path}"
  end
end
overwrite_routes() click to toggle source
# File lib/generators/routes.rb, line 1
def overwrite_routes
  # find the routes
  route_path = "config/routes.rb"

  # text that will overwrite namespace :admin do
  to_add = """  resources :legals, only: [:show]
  namespace :admin do
    resources :legals,                             except: [:show] do
      collection { patch :sort }
    end
  """

  new_routes_text = ""
  routes = File.open(route_path).read
  routes.each_line do |line|
    # add routes for legal
    if line.include?("namespace :admin do")
      new_routes_text += to_add
    else
      new_routes_text += line
    end
  end

  # rewrite the routes file
  File.write(route_path, new_routes_text)

  puts "++ Changed -> #{route_path}"
end
print_manual(type) click to toggle source
run_generator(get_type) click to toggle source
# File lib/bamboo_legals.rb, line 10
def run_generator(get_type)
  if get_type == "monolingual" or get_type == "mono"
    type = "monolingual"

    `rails g model Legal title:string content:text is_visible:boolean position:integer`
  elsif get_type == "multilingual" or get_type == "multi"
    type = "multilingual"

    `rails g model Legal is_visible:boolean position:integer`
    `rails g model LegalTranslation title:string content:text language_id:string position:integer legal_id:integer`
  end

  if type 

    # get path to /home/mislav/.rvm/gems/ruby-2.3.1/gems/bamboo_legals-0.1.1/lib/templates
    temp_path = "#{File.dirname(__FILE__)}/templates"

    overwrite_migration
    overwrite_model(type, temp_path)

    create_admin_controller(type, temp_path)
    create_controller(type, temp_path)
    create_admin_views(type, temp_path)
    create_views(type, temp_path)

    overwrite_routes
    print_manual(type)
  end

end