From 6347f494835be0e9ae8a91c297e87d0aaf9509de Mon Sep 17 00:00:00 2001 From: trogdoro Date: Tue, 19 Nov 2013 10:40:18 -0800 Subject: [PATCH] Rails: small improvements. --- menu/rails.rb | 188 ++++++++++++++++++++++++++++++------------- menu/ruby/index.menu | 2 + menu/rvm.menu | 8 +- menu/sinatra.menu | 12 ++- 4 files changed, 152 insertions(+), 58 deletions(-) diff --git a/menu/rails.rb b/menu/rails.rb index 6a6068e7..e25f3661 100644 --- a/menu/rails.rb +++ b/menu/rails.rb @@ -8,14 +8,22 @@ class Rails - Show options: Rails.menu > + MENU_HIDDEN = " + .running dir/ + .nest/ + " + MENU = " - .start/ - .generate/ - app/ + - plugin/ - model/ - resource/ - controller/ - scaffold/ + - .inspect/ + - .routes/ - .interact/ - .rails console/ - .sqlite console/ @@ -27,24 +35,43 @@ class Rails - .rails version/ " + def self.nest + Tree.to_parent + Tree.kill_under + Line.delete + txt = " + /tmp/rails1/ + @rails/ + ".unindent + + View.<< txt, :dont_move=>1 + Line.next + Line.to_beginning + "" + end + + def self.menu_before *path dir = Tree.closest_dir yield[:ancestors] - # Rethink about how to use default - really just do it with no parent? - # No, probably add path of default project before line... - # /projects/rails2/@rails/ - # Don't intercede if already rails app or trying to generate - return nil if ["generate", "general"].member?(path[0]) || File.exists?("#{dir}/app") + # return nil if ["generate", "general"].member?(path[0]) || File.exists?("#{dir}/app") + return nil if ["generate", "nest"].member?(path[0]) || File.exists?("#{dir}/app") + + # Not nested, so offer to nest... - # If not a rails dir, give option to generate return " - > No rails app in #{dir} yet. Generate it? - - generate/app/ + > The 'rails' menu must be nested under a dir? + | Do it for you? + - nest/ + " if ! dir + + # Not a rails dir, so offer to nest under a dir... - > Non project-specific options - - general/ + return " + > Rails app doesn't exist. Generate it? + - generate/app/ " end @@ -73,19 +100,19 @@ def self.use_rspec end def self.sqlite_console - Console.run "sqlite3 db/development.sqlite3", :dir=>Projects.default, :buffer=>"sqlite console" - ".flash - opened console!" + Console.run "sqlite3 db/development.sqlite3", :dir=>self.dir(yield), :buffer=>"sqlite console", :dont_move=>1 + "@flash/- opened console in other view!" end def self.rails_console - Console.run "rails c", :dir=>Projects.default, :buffer=>"rails console" - ".flash - opened console!" + Console.run "rails c", :dir=>self.dir(yield), :buffer=>"rails console", :dont_move=>1 + "@flash/- opened console in other view!" end def self.generate what, name=nil, detail=nil examples = " - > Example fields + > 1. Example fields | name:string | details:text | summary:text @@ -94,28 +121,60 @@ def self.generate what, name=nil, detail=nil | delivery:boolean | purchased_at:datetime | user:references + | --no-timestamps ".unindent + dir = self.dir(yield) + + options = yield + case what when "app" - Console.run "rails new . --skip-bundle", :dir=>Projects.default - return "- generating rails app..." + dir, stem = File.dirname(dir), File.basename(dir) + + Console.run "rails new \"#{stem}\" --skip-bundle", :dir=>dir, :dont_move=>1 + return "- generating rails app in other view..." + when "plugin" + Console.run "rails plugin new . --skip-bundle --full", :dir=>dir, :dont_move=>1 + return "- generating rails app in other view..." when "model", "resource", "scaffold" return View.prompt "Enter a name" if ! name return examples if ! detail fields = Tree.txt.gsub("\n", ' ').strip - Console.run "rails g #{what} #{name} #{fields}", :dir=>Projects.default - return "- generating #{what}..." + Console.run "rails g #{what} #{name} #{fields}", :dir=>dir, :dont_move=>1 + Ol.a options + dir = options[:ancestors][-1] + return " + | Generating #{what} in other view. Now run the migrations: + @#{dir}% rake db:migrate + | Then restart the server + @current/rails server + | The url + @http://localhost:3000/#{name} + " when "controller" return View.prompt "Enter a name" if ! name return View.prompt "Enter an action" if ! detail - Console.run "rails g controller #{name} #{detail}", :dir=>Projects.default - return "- generating controller..." + Console.run "rails g controller #{name} #{detail}", :dir=>dir, :dont_move=>1 + return " + - Generating controller in other view... + @http://localhost:3000/#{name}/#{detail} + @#{dir}/app/ + - controllers/#{name}_controller.rb + - views/#{name}/#{detail}.html.erb + " end "- Don't know how to generate a '#{what}'!" end + def self.dir options={} + + # Nevermind > If no args, hit :3000 to get dir + + Tree.closest_dir options[:ancestors] + end + def self.start *args # If 1st arg is number, assume it's the port @@ -124,26 +183,31 @@ def self.start *args # If 'browse', just bring up in browser if args == ['browse'] Firefox.url "http://localhost:#{port || 3000}/" - return ".flash - opened in browser!" + return "@flash/- opened in browser!" end command = "rails s" command << " -p #{port}" if port - Console.run command, :dir=>Projects.default, :buffer=>"rails server" + Console.run command, :dir=>self.dir(yield), :buffer=>"rails server", :dont_move=>1 + # Console.run command, :dir=>self.dir(yield), :buffer=>"rails server" # Check whether it's already running "| Rails app was already running\n- browse/" - "| Starting rails app...\n- browse/" + "| Starting rails app in other view...\n- browse/" end - def self.command txt - Console.run txt, :dir=>Projects.default + def self.command txt, options + Console.run txt, :dir=>self.dir(options) end def self.migrate - self.command "rake db:migrate" + self.command "rake db:migrate", yield + end + + def self.routes + self.command "rake routes", yield end def self.eval *args @@ -172,30 +236,50 @@ def self.run_in_app txt, options={} if options[:yaml] # If yaml passed, deduce code to save model, and run... - txt = " + txt = %` txt = #{txt.inspect} mods = YAML::load(txt) mods = [mods] if ! mods.is_a?(Array) mods.each do |mod| - mod.partial_updates = false + mod.instance_variable_set('@partial_writes', false) existing = mod.class.where :id=>mod.id mod.instance_variable_set('@new_record', true) if existing.empty? mod.save end - " + ` end File.open("/tmp/rails_run_tmp.txt", "w") { |f| f << txt } - response = HTTParty.get("http://localhost:3000/xiki_dev") rescue :exception + response = HTTParty.get("http://localhost:3000/xikidev") rescue :exception return "| The rails server doesn't appear to be running. Start default server?\n@rails/start/" if response == :exception - return self.suggest_creating_controller if response.response.is_a?(Net::HTTPNotFound) + if response.response.is_a?(Net::HTTPNotFound) + + # # If migrations error, suggest running it + # if txt =~ /

Migrations are pending/ + # return " + # > Migrations need to be run... + # @#{self.running_dir}/ + # @rails/ + # " + # end + + return self.suggest_installing_plugin(options) if response.body =~ /

No route matches .+\/xikidev/ + + # return response.body[/

(.+)<\/h2>/] + return response.body[/

(.+)/, 1].sub(/<\/h2>.*/, '').gsub("'", "'") + end + + # Yaml data launched, so just pretend like it was saved?... + # What about displaying errors? + + return "@flash/- saved!" if options[:yaml] + - return ".flash - saved!" if options[:yaml] txt = response.body txt.gsub!(/ +$/, '') @@ -204,41 +288,35 @@ def self.run_in_app txt, options={} txt.sub!(/.+?

/m, '

') if txt =~ /
/ && txt =~ /

/ # If file not found, suggest generating it - if txt =~ /uninitialized constant XikiDevController::(\w+)/ + if txt =~ /uninitialized constant XikidevController::(\w+)/ clazz = $1 return " > Class '#{clazz}' doesn't exist. Generate it as a model? - @#{Projects.default} + @#{self.running_dir}/ @rails/generate/model/#{clazz} " - end txt - end - def self.suggest_creating_controller + def self.running_dir + HTTParty.get("http://localhost:3000/xikidev/dir").body rescue :exception + end + def self.suggest_installing_plugin options + # Duh, can't get it yet because can't talk to app that doesn't have controller + dir = self.running_dir %` - | This rails app may not have the xiki dev controller - | installed to let xiki evaluate code in it in dev mode. - | Create it? - @#{Projects.default} - - app/controllers/ - - xiki_dev_controller.rb - | class XikiDevController < ApplicationController - | def index - | return render(:text=>"Disabled unless development and called locally.") if ! Rails.env.development? || request.remote_ip != "127.0.0.1" - | code = File.read "/tmp/rails_run_tmp.txt" - | txt = eval code - | render :text=>txt.to_yaml - | end - | end - - config/routes.rb - |+ match 'xiki_dev' => 'xiki_dev#index' - | end + | The running rails app doesn't appear to have the Xiki plugin + | installed. This plugin lets Xiki evaluate code in it in dev mode. + @/path/to/your/rails/app/ + - 1. Add this line: + - Gemfile + | gem 'xiki-rails' + - 2. Run bundler: + % bundle install ` end diff --git a/menu/ruby/index.menu b/menu/ruby/index.menu index 4b4b9dfc..600338bb 100644 --- a/menu/ruby/index.menu +++ b/menu/ruby/index.menu @@ -1,3 +1,5 @@ - .classes/ - load path/ ! $LOAD_PATH.map{|o| "@#{o}/\n"}.join("") +- version/ + ! RUBY_VERSION diff --git a/menu/rvm.menu b/menu/rvm.menu index 4c684f99..05a0b32f 100644 --- a/menu/rvm.menu +++ b/menu/rvm.menu @@ -1,5 +1,11 @@ - list/ - @$ rvm list + - currently installed/ + @$ rvm list + - available/ + - 1) Update rvm so it knows about new ones + @$ rvm get stable + - 2) List them + @$ rvm list known - set default/ @% rvm --default use system @% rvm --default use 1.8.7 diff --git a/menu/sinatra.menu b/menu/sinatra.menu index d01a4b50..0d38671a 100644 --- a/menu/sinatra.menu +++ b/menu/sinatra.menu @@ -2,13 +2,12 @@ @/tmp/ - sinatra_hi.rb | require 'sinatra' - | | get '/' do | "Hello Booze!" | end % ruby -rubygems sinatra_hi.rb @http://localhost:4567/ -- hello world OO/ +- as a class/ @/tmp/ % ruby -rubygems sinatra_oo.rb - sinatra_oo.rb @@ -26,6 +25,15 @@ | | SinatraOo.run! @http://localhost:4567/hello +- wildcard path/ + @/tmp/ + - sinatra_path.rb + | require 'sinatra' + | get '/*' do |path| + | "The path is: #{path.inspect}" + | end + % ruby -rubygems sinatra_hi.rb + @http://localhost:4567/foo/bar - docs/ > Intro @http://www.sinatrarb.com/intro