Created
November 21, 2012 19:07
-
-
Save emtee/4126950 to your computer and use it in GitHub Desktop.
Template for creating a new rails app with pre-configured active admin + necessary migration and associations. It relies on a yml file to find out the model related information. Checkout the demo yml file here https://gist.github.com/4127296
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # rails new maverick -m https://raw.github.com/gist/4126950/ -d mysql | |
| # rake rails:template LOCATION=https://raw.github.com/gist/4126950/ | |
| # <------ Pre-requisits ------> | |
| # Setting current directory (for loading the yml config file) | |
| @current_dir = Dir.pwd.gsub("/#{app_name}", "") | |
| def ask_wizard(question) | |
| ask "\033[1m\033[30m\033[46m" + "prompt".rjust(10) + "\033[1m\033[36m" + " #{question}\033[0m" | |
| end | |
| def yes_wizard?(question) | |
| answer = ask_wizard(question + " \033[33m(y/n)\033[0m") | |
| case answer.downcase | |
| when "yes", "y", "" | |
| true | |
| when "no", "n" | |
| false | |
| # else #allowing user to hit enter and pass the check | |
| # yes_wizard?(question) | |
| end | |
| end | |
| def unfullfilled_pre_requisits | |
| run "rm -rf #{Dir.pwd}" | |
| raise "aborted because of unfullfilled Pre-requisits..." | |
| end | |
| # <------ Pre-requisits end ------> | |
| # ----------------------------- Configure a new application ---------------------------------------------- | |
| def configure_new_app | |
| say "You need to provide a yml file having models and model-attributes and model-relationships, to use 'Maverick'.", :red | |
| say "Download a demo yml file from https://raw.github.com/gist/4127296/", :red | |
| say "" | |
| unless yes_wizard?("Have you provided the yml file in the current directory? eg. if your app name is 'test', provide a yml namely test.yml in #{@current_dir}") | |
| unfullfilled_pre_requisits | |
| end | |
| unless yes_wizard?("Do you have a proper installation of ruby(or rvm) and rails") | |
| unfullfilled_pre_requisits | |
| end | |
| models = YAML.load_file("#{@current_dir}/#{app_name}.yml")[:models] # just to make the process end if yml file is not provided | |
| gem "activeadmin" # adding activeadmin gem to gemfile | |
| run "bundle" | |
| mysql_username = ask_wizard("Please enter you mysql Username...") | |
| mysql_password = ask_wizard("Please enter you mysql Password...") | |
| gsub_file "config/database.yml", /username: root/, "username: #{mysql_username}" | |
| gsub_file "config/database.yml", /password:/, "password: #{mysql_password rescue ''}" | |
| affirm = yes_wizard? "Drop any existing databases named #{app_name}?" | |
| if affirm | |
| run 'bundle exec rake db:drop' | |
| else | |
| raise "aborted at user's request" | |
| end | |
| rake "db:create" | |
| run "rails generate active_admin:install" | |
| @migrations = [] | |
| models = YAML.load_file("#{@current_dir}/#{app_name}.yml")[:models] | |
| models.each do |model, attr| | |
| model_name = model.titleize.gsub(" ","") + " " | |
| attr[:fields].each do |column| | |
| model_name += column.split("-").join(":") + " " | |
| end | |
| run "rails g model #{model_name}" | |
| attr[:associations].each do |a| | |
| relation, entity = a.split("~") | |
| if relation == "has_one" || relation == "has_many" | |
| # @model_changes[entity] = " belongs_to :#{model}" | |
| elsif relation == "has_and_belongs_to_many" | |
| item1, item2 = [entity, model].sort!{|t1,t2|t1 <=> t2} | |
| table_name = "#{item1.pluralize}_#{item2.pluralize}" | |
| # To avoid genertion of duplicate migrations while creating HABTM relation | |
| unless @migrations.include?(table_name) | |
| @migrations << table_name | |
| filename = ((Time.now.utc).strftime("%Y%m%d%H%M%S").to_i - 55).to_s + "_create_" + table_name | |
| classname = "Create" + item1.pluralize.titleize + item2.pluralize.titleize | |
| create_file "db/migrate/#{filename}.rb" do | |
| <<-CODE | |
| class #{classname} < ActiveRecord::Migration | |
| def change | |
| create_table '#{table_name}', :id => false do |t| | |
| t.column :#{model.singularize}_id, :integer | |
| t.column :#{entity.singularize}_id, :integer | |
| end | |
| add_index :#{table_name}, :#{model.singularize}_id | |
| add_index :#{table_name}, :#{entity.singularize}_id | |
| end | |
| end | |
| CODE | |
| end | |
| end | |
| # @model_changes[entity] = " has_and_belongs_to_many :#{model.pluralize}" | |
| elsif relation == "has_many_through" | |
| # @model_changes[entity] = " has_many :somethings, :through => otherthing" | |
| elsif relation == "belongs_to" | |
| inject_into_file "app/models/#{model}.rb", " attr_accessible :#{entity}_id\n", :after => "class #{model.titleize} < ActiveRecord::Base\n" | |
| run "rails g migration add_#{entity}_reference_to_#{model} #{entity}_id:integer" | |
| end | |
| entity = entity.to_sym | |
| say " #{relation} :#{entity}\n" | |
| inject_into_file "app/models/#{model}.rb", " #{relation} :#{entity}\n", :after => "class #{model.titleize} < ActiveRecord::Base\n" | |
| end | |
| end | |
| rake "db:migrate" | |
| models.each do |model, fields| | |
| run "rails generate active_admin:resource #{model.titleize.gsub(" ","")}" | |
| end | |
| run "rm public/index.html" # removing the index file to enable root_url to work | |
| route "root :to => 'admin/dashboard#index'" | |
| end | |
| # ----------------------------- Update Existing application ---------------------------------------------- | |
| def create_table | |
| end | |
| def ask_wizard(question) | |
| ask "\033[1m\033[30m\033[46m" + "prompt".rjust(10) + "\033[1m\033[36m" + " #{question}\033[0m" | |
| end | |
| @columns = [] | |
| def table_columns | |
| answer = ask_wizard("Please enter a field with datatype: (eg. name:string)") | |
| unless answer == "x" | |
| arr = answer.split(":") | |
| arr.delete("") | |
| if arr.size == 1 || @columns.include?(answer) | |
| say "Invalid data!!", :red | |
| else | |
| @columns << answer | |
| end | |
| table_columns | |
| end | |
| return @columns | |
| end | |
| def add_model | |
| say "" | |
| model = ask_wizard("Please enter model name:").singularize.titleize | |
| say "Enter 'x' anytime, to exit & generate the model...." | |
| @columns = [] | |
| @columns = table_columns | |
| # create_table(model, @columns) | |
| run "rails g model #{model} #{@columns.join(' ')}" | |
| run "rails generate active_admin:resource #{model}" | |
| # raise @columns.inspect | |
| end | |
| def add_association | |
| say "Please specify the association in one of the following formats:" | |
| say "modelA has_one modelB" | |
| say "modelA belongs_to modelB" | |
| say "modelA has_many modelB" | |
| say "modelA has_and_belongs_to_many modelB" | |
| say "modelA has_many_through modelB, modelC" | |
| association = ask_wizard("Please enter the desired association:") | |
| raise association.inspect | |
| end | |
| def destroy_model | |
| model = ask_wizard("Please enter model name:").singularize.titleize | |
| run "rails d active_admin:resource #{model}" | |
| run "rails d model #{model}" | |
| end | |
| def display_options | |
| say "" | |
| say "Here you can select from many of the options", :green | |
| say "" | |
| say "Available options:" | |
| say "1) Add model" | |
| say "2) Remove model" | |
| say "3) Add fields to table" | |
| say "4) Remove fields to table" | |
| say "5) Change field's datatype for a table" | |
| say "6) Add Association to a model" | |
| say "7) Remove Association from model" | |
| say "8) Exit and run rake db:migration" | |
| end | |
| def restart_wizard | |
| display_options | |
| changes_wizzard | |
| end | |
| def changes_wizzard | |
| selected = ask_wizard("Select an option: ") | |
| case selected | |
| when "1" | |
| add_model | |
| restart_wizard | |
| when "2" | |
| destroy_model | |
| restart_wizard | |
| when "6" | |
| add_association | |
| when "8" | |
| run "rake db:migrate" | |
| else | |
| changes_wizzard() | |
| end | |
| end | |
| # ---------------------------------------------------------------------------------------------------------------- | |
| say "\nSelect an option:" | |
| say "1) Create a new application." | |
| say "2) Make changes to my existing application." | |
| choice = ask("You Choice:") | |
| if choice == "1" | |
| configure_new_app | |
| elsif choice == "2" | |
| display_options | |
| changes_wizzard() | |
| else | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment