class OldSql::InstallGenerator

Public Instance Methods

check_for_devise() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 8
def check_for_devise
  puts "Old SQL works with devise. Checking for a current installation of devise!\n"

  if defined?(Devise)
    check_for_devise_models
  else
    puts "Please put gem 'devise' into your Gemfile"
  end
  
  puts "Also you need a new migration. We'll generate it for you now."
  invoke 'old_sql:install_migrations', ["#{model_name}"]
end
configure_initializer() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 64
def configure_initializer
  initializer_path = "#{app_path}/config/initializers/old_sql.rb"
  gsub_file initializer_path, /DeviseModel/, "#{model_name.singularize.downcase}"
end
copy_initializer() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 21
def copy_initializer
  template "old_sql.rb", "config/initializers/old_sql.rb"
end
copy_locales_files() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 25
def copy_locales_files
  print "Now copying locales files! "
  ###
  locales_path = "#{gem_path}/config/locales/*.yml"

  locales_app_path = "#{app_path}/config/locales"

  unless File.directory?(locales_app_path)
    FileUtils.mkdir locales_app_path
  end

  Dir.glob(locales_path).each do |file|
    file_path = file.split("/")
    file_path = file_path[-1]
    FileUtils.copy_file(file, "#{locales_app_path}/#{file_path}")
    print "."
  end
  print "\n"

end
copy_old_sql_files() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 54
def copy_old_sql_files
  path_to_reports_config = "#{app_path}/config/old_sql/reports.yml"
  copy_file "reports.yml.example", path_to_reports_config unless File.exists?(path_to_reports_config)
  copy_file "user.erb.example", "#{app_path}/config/old_sql/report_sql/user_old_sql_demo.erb"
  copy_file "user_processor.rb.example", "#{app_path}/lib/old_sql/report_processor/user_old_sql_demo_processor.rb"
  copy_file "user_design_template.csv", "#{app_path}/config/old_sql/report_design/user_old_sql_demo.csv"
  copy_file "user_old_sql_demo_chart_design.yml", "#{app_path}/config/old_sql/report_design/user_old_sql_demo.yml"
  copy_file "test_db.rb", "#{app_path}/lib/old_sql/db/test.rb"
end
create_old_sql_dirs() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 46
def create_old_sql_dirs
  empty_directory "#{app_path}/config/old_sql/"
  empty_directory "#{app_path}/config/old_sql/report_sql"
  empty_directory "#{app_path}/config/old_sql/report_design"
  empty_directory "#{app_path}/lib/old_sql/report_processor"
  empty_directory "#{app_path}/lib/old_sql/db"
end

Private Instance Methods

add_devise_to_routes() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 141
def add_devise_to_routes
  routes_path = "#{app_path}/config/routes.rb"
  if open(routes_path).grep(/devise_for :#{model_name}/).count<=0
    puts "Adding devise_for :#{model_name.pluralize} to #{routes_path}"
    insert_into_file routes_path, "  devise_for :#{model_name.pluralize}\n\n", :after => "Application.routes.draw do\n"
  end
end
app_path() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 174
def app_path
  app_path = Rails.public_path.split("/")
  app_path.delete_at(-1)
  app_path = app_path.join("/")
  app_path
end
check_for_devise_models() click to toggle source

PRIVATE ################

# File lib/generators/old_sql/install_generator.rb, line 73
def check_for_devise_models
  # File.exists?
  devise_path =  "#{FileUtils.pwd}/config/initializers/devise.rb"

  if File.exists?(devise_path)
    parse_route_files
  else
    puts "Looks like you don't have devise install! We'll install it for you!"
    
    invoke 'devise:install'
    
    if !devise_table_exists?
      puts 'Devise Model Does Not Exist'
      set_devise
    elsif !model_has_devise?
      puts 'Devise Model Does Not Have Devise Support'
      invoke "old_sql:install_devise_migrations", ["#{model_name}"]
      create_model_class unless model_exists?
      add_devise_to_routes
    end

  end
end
create_model_class() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 135
def create_model_class
  model_path = "#{app_path}/app/models/#{model_name.singularize}.rb"
  copy_file "devise_model.rb.template", model_path
  gsub_file model_path, /DeviseModel/, "#{model_name.singularize.capitalize}"
end
devise_table_exists?() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 97
def devise_table_exists?
  app_path = Rails.public_path.split("/")
  app_path.delete_at(-1)
  app_path = app_path.join("/")
  schema_path = "#{app_path}/db/schema.rb"
  
  puts "Checking #{schema_path} for pattern #{model_name}" 
  
  if File.exists?(schema_path) && open(schema_path).grep(/#{model_name}/).count>0
    return true
  else
    return false
  end
end
gem_path() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 181
def gem_path
  gem_path = __FILE__
  gem_path = gem_path.split("/")
  gem_path = gem_path[0..-5]
  gem_path = gem_path.join("/")
  gem_path
end
model_exists?() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 127
def model_exists?
  app_path = Rails.public_path.split("/")
  app_path.delete_at(-1)
  app_path = app_path.join("/")
  
  File.exists?("#{app_path}/app/models/#{model_name.pluralize}.rb")
end
model_has_devise?() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 112
def model_has_devise?
  app_path = Rails.public_path.split("/")
  app_path.delete_at(-1)
  app_path = app_path.join("/")
  schema_path = app_path+'/db/schema.rb'

  puts "Checking #{schema_path} for pattern /database_authenticatable/"

  if File.exists?(schema_path) && open(schema_path).grep(/database_authenticatable/).count>0
    return true
  else
    return false
  end
end
parse_route_files() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 149
def parse_route_files
  # check if migrations exist
  routes_path = "#{app_path}/config/routes.rb"

  content = ""

  File.readlines(routes_path).each{|line|
    content += line
  }

  unless content.index("devise_for").nil?
    # there is a devise_for in routes => Do nothing
    puts "Great! You have devise installed and setup!"
  else
    puts "Great you have devise installed, but not set up!"
    set_devise
  end
end
set_devise() click to toggle source
# File lib/generators/old_sql/install_generator.rb, line 168
    def set_devise
      puts "Setting up devise for you!
======================================================"
      invoke 'devise', [model_name]
    end