tiny-rails 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,12 +3,26 @@
3
3
  Scaffold for tiny Rails apps based on José Valim's Rails Lightweight Stack
4
4
  [code](https://gist.github.com/1942658)
5
5
 
6
+
6
7
  ## Installation
7
8
 
8
9
  Install it using:
9
10
 
10
11
  $ gem install tiny-rails
11
12
 
13
+
14
+ ## WTF?! Why would I use this?
15
+
16
+ Although the generated application code could be used on a production server,
17
+ the idea is to try to give you a really basic application to try out new Rails
18
+ gems, create spikes and to provide an isolated small Rails environment for
19
+ reproducing bugs to support bug reports.
20
+
21
+ You could also use this to create a single page application with all rails
22
+ features like code reloading and the asset pipeline without having to set up
23
+ a Sinatra application from the ground app.
24
+
25
+
12
26
  ## Usage
13
27
 
14
28
  ```terminal
@@ -28,6 +42,10 @@ This will give you a pretty basic application that you can run with `rackup`
28
42
  or you prefferend server. It even supports code reloading for the generated
29
43
  controller!
30
44
 
45
+ You can also fire up a console to play around with the generated app running
46
+ `tiny-rails console`. If you want to use Pry, you can just add the `pry-rails`
47
+ gem to your Gemfile.
48
+
31
49
 
32
50
  ## Addons
33
51
 
@@ -54,6 +72,9 @@ folder.
54
72
  Here's a list of the addons bundled with the gem:
55
73
 
56
74
  * [activerecord](https://github.com/fgrehm/tiny-rails/blob/master/addons/activerecord.rb)
75
+ * [coffeescript](https://github.com/fgrehm/tiny-rails/blob/master/addons/coffeescript.rb)
76
+ * [jquery](https://github.com/fgrehm/tiny-rails/blob/master/addons/jquery.rb)
77
+ * [client_side_validations](https://github.com/fgrehm/tiny-rails/blob/master/addons/client_side_validations.rb)
57
78
 
58
79
 
59
80
  ### Building your own addon
@@ -2,13 +2,16 @@ gem 'activerecord', '~> 3.2'
2
2
  gem 'sqlite3'
3
3
 
4
4
  template 'activerecord/models.rb', 'models.rb'
5
+
5
6
  require_models_code = <<-CODE
6
7
  # Enable code reloading for models
7
8
  require_dependency 'models'
8
9
  CODE
9
- inject_into_file 'tiny_rails_controller.rb', "\n#{require_models_code}", :after => /class TinyRailsController < ActionController::Base/
10
+ inject_into_file 'application_controller.rb', "\n#{require_models_code}", :after => /class ApplicationController < ActionController::Base/
10
11
 
11
- config_db_code = <<-CODE
12
+ application <<-CODE
13
+ # We need to override the configuration method here, otherwise Rails will
14
+ # try to load the yaml configuration file at config/database.yml
12
15
  def config.database_configuration
13
16
  {
14
17
  'development' =>
@@ -21,7 +24,8 @@ config_db_code = <<-CODE
21
24
  }
22
25
  end
23
26
  CODE
24
- application "\n#{config_db_code}"
27
+
28
+ inject_into_file 'boot.rb', "\nrequire \"active_record/railtie\"", :after => /require ['"]action_controller\/railtie['"]/
25
29
 
26
30
  template 'activerecord/migrate', 'migrate'
27
31
  chmod 'migrate', 0755
@@ -7,13 +7,4 @@ gem 'client_side_validations'
7
7
  sentinel = {:after => "#= require jquery\n"}
8
8
  inject_into_file 'application.coffee', "#= require rails.validations\n", sentinel
9
9
 
10
- initializer_code = <<-CODE
11
- ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
12
- unless html_tag =~ /^<label/
13
- %{<div class="field_with_errors">\#{html_tag}<label for="\#{instance.send(:tag_id)}" class="message">\#{instance.error_message.first}</label></div>}.html_safe
14
- else
15
- %{<div class="field_with_errors">\#{html_tag}</div>}.html_safe
16
- end
17
- end
18
- CODE
19
- initializer initializer_code
10
+ initializer 'require "tiny-rails/app/initializers/client_side_validations"'
@@ -32,19 +32,23 @@ module TinyRails
32
32
  end
33
33
  end
34
34
 
35
- # Adds a line inside the Application class on boot.rb.
35
+ # Appends a line inside the TinyRailsApp class on boot.rb.
36
36
  #
37
37
  # application do
38
38
  # "config.assets.enabled = true"
39
39
  # end
40
40
  def application(data=nil, &block)
41
- sentinel = /class TinyRailsApp < Rails::Application/i
42
41
  data = block.call if !data && block_given?
43
42
 
44
- inject_into_file 'boot.rb', "\n #{data}", :after => sentinel
43
+ data = "\n#{data}" unless data =~ /^\n/
44
+ data << "\n" unless data =~ /\n$/
45
+
46
+ inject_into_file 'boot.rb', data, :after => /^ config\.secret_token = .+\n/
45
47
  end
46
48
 
47
49
  def initializer(data)
50
+ data = open(data) { |io| io.read } if data =~ /https?:\/\//
51
+
48
52
  if File.exists? 'initializers.rb'
49
53
  append_file 'initializers.rb', "\n#{data}"
50
54
  else
@@ -52,16 +56,29 @@ module TinyRails
52
56
  end
53
57
  end
54
58
 
59
+ def migration(data)
60
+ data << "\n" unless data =~ /\n$/
61
+
62
+ inject_into_file 'migrate', data, :after => /^ActiveRecord::Schema\.define do\n/
63
+ end
64
+
65
+ def route(new_route)
66
+ new_route << "\n" unless new_route =~ /\n$/
67
+
68
+ inject_into_file 'boot.rb', new_route, :after => /TinyRailsApp\.routes\.draw do\n/
69
+ end
70
+
55
71
  # Self explanatory :P
56
72
  def enable_asset_pipeline!
57
73
  return if File.read('boot.rb') =~ /^ config\.assets\.enabled = true$/
58
74
 
59
- code = <<-CONFIG
75
+ application <<-CONFIG
76
+ # Enable asset pipeline
60
77
  config.assets.enabled = true
61
78
  config.assets.debug = true
62
79
  config.assets.paths << File.dirname(__FILE__)
63
80
  CONFIG
64
- application "\n#{code}"
81
+ inject_into_file 'boot.rb', "\nrequire \"sprockets/railtie\"", :after => /require ['"]action_controller\/railtie['"]/
65
82
  end
66
83
 
67
84
  def addon(path)
@@ -0,0 +1,7 @@
1
+ ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
2
+ unless html_tag =~ /^<label/
3
+ %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
4
+ else
5
+ %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
6
+ end
7
+ end
@@ -2,8 +2,9 @@ require 'thor'
2
2
  require 'thor/group'
3
3
 
4
4
  require 'tiny-rails/actions'
5
- require 'tiny-rails/commands/new'
6
5
  require 'tiny-rails/commands/add'
6
+ require 'tiny-rails/commands/console'
7
+ require 'tiny-rails/commands/new'
7
8
 
8
9
  module TinyRails
9
10
  class CLI < Thor
@@ -21,5 +22,10 @@ module TinyRails
21
22
  addons = Array(addons)
22
23
  Commands::Add.start(addons)
23
24
  end
25
+
26
+ desc 'console', 'Starts the tiny-rails console'
27
+ def console
28
+ Commands::Console.start
29
+ end
24
30
  end
25
31
  end
@@ -0,0 +1,26 @@
1
+ module TinyRails
2
+ module Commands
3
+ class Console < Thor::Group
4
+ include Thor::Actions
5
+ include Actions
6
+
7
+ # TODO: Move to a base command
8
+ def self.source_root
9
+ "#{File.expand_path('../../../../templates', __FILE__)}/"
10
+ end
11
+
12
+ def guard_inside_tiny_rails_app
13
+ unless File.exists?('boot.rb')
14
+ puts "Can't start console from outside a TinyRails application, please change to a TinyRails application directory first.\n"
15
+ exit(1)
16
+ end
17
+ end
18
+
19
+ def start_console
20
+ require './boot.rb'
21
+ require 'rails/commands/console'
22
+ Rails::Console.start(TinyRailsApp)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -22,7 +22,7 @@ module TinyRails
22
22
  .gitignore
23
23
  Gemfile
24
24
  boot.rb
25
- tiny_rails_controller.rb
25
+ application_controller.rb
26
26
  index.html.erb
27
27
  server
28
28
  config.ru
@@ -1,3 +1,3 @@
1
1
  module TinyRails
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -55,12 +55,12 @@ describe TinyRails::Actions do
55
55
  describe '#application' do
56
56
  let(:boot_rb) { File.read 'boot.rb' }
57
57
 
58
- before { action :create_file, 'boot.rb', "class TinyRailsApp < Rails::Application\nend" }
58
+ before { action :create_file, 'boot.rb', "class TinyRailsApp\n config.secret_token = 'something'\nend" }
59
59
 
60
60
  it 'includes data in TinyRailsApp definition' do
61
- assets_enable = 'config.assets.enabled = true'
61
+ assets_enable = ' config.assets.enabled = true'
62
62
  action :application, assets_enable
63
- boot_rb.should =~ /class TinyRailsApp < Rails::Application\n #{Regexp.escape(assets_enable)}/
63
+ boot_rb.should =~ /class TinyRailsApp\n config\.secret_token = 'something'\n\n#{Regexp.escape(assets_enable)}/
64
64
  end
65
65
 
66
66
  it 'includes provided block contents in TinyRailsApp definition' do
@@ -74,11 +74,25 @@ describe TinyRails::Actions do
74
74
  end
75
75
  end
76
76
 
77
+ describe '#route' do
78
+ let(:boot_rb) { File.read 'boot.rb' }
79
+
80
+ before do
81
+ action :create_file, 'boot.rb', "TinyRailsApp.routes.draw do\nend"
82
+ end
83
+
84
+ it 'includes data in routes definitions' do
85
+ new_route = ' match "foo" => "tiny_rails#bar"'
86
+ action :route, new_route
87
+ boot_rb.should =~ /TinyRailsApp\.routes.draw do\n#{Regexp.escape(new_route)}end/
88
+ end
89
+ end
90
+
77
91
  describe '#enable_asset_pipeline!' do
78
92
  let(:boot_rb) { File.read 'boot.rb' }
79
93
 
80
94
  before do
81
- action :create_file, 'boot.rb', "class TinyRailsApp < Rails::Application\nend"
95
+ action :create_file, 'boot.rb', "require \"action_controller/railtie\"\nclass TinyRailsApp\n config.secret_token = 'something'\nend"
82
96
  action :enable_asset_pipeline!
83
97
  end
84
98
 
@@ -94,6 +108,10 @@ describe TinyRails::Actions do
94
108
  boot_rb.should =~ /^ config\.assets\.paths << File\.dirname\(__FILE__\)$/
95
109
  end
96
110
 
111
+ it 'require sprockets railtie' do
112
+ boot_rb.should =~ /^require "sprockets\/railtie"/
113
+ end
114
+
97
115
  it 'does not duplicate configs' do
98
116
  10.times { action :enable_asset_pipeline! }
99
117
  boot_rb.should have_a_single_occurence_of('config.assets.enabled = true')
@@ -102,9 +120,11 @@ describe TinyRails::Actions do
102
120
 
103
121
  describe '#initializer' do
104
122
  let(:initializers_rb) { File.read 'initializers.rb' }
123
+ let(:remote_code) { StringIO.new('remote code') }
105
124
 
106
125
  before do
107
126
  action :initializer, '# Ruby code...'
127
+ generator.stub(:open).and_yield(remote_code)
108
128
  end
109
129
 
110
130
  it 'creates an initializers.rb file if it doesnt exist' do
@@ -115,5 +135,23 @@ describe TinyRails::Actions do
115
135
  action :initializer, '# More code...'
116
136
  initializers_rb.should =~ /\n# More code.../
117
137
  end
138
+
139
+ it 'supports appending a remote file' do
140
+ action :initializer, 'http://path.to/gist'
141
+ initializers_rb.should =~ /\nremote code/
142
+ end
143
+ end
144
+
145
+ describe '#migration' do
146
+ let(:migrate) { File.read 'migrate' }
147
+
148
+ before do
149
+ action :create_file, 'migrate', "ActiveRecord::Schema.define do\nend"
150
+ action :migration, ' create_table :users'
151
+ end
152
+
153
+ it 'appends to schema definition on migrate file' do
154
+ migrate.should =~ /ActiveRecord::Schema.define do\n create_table :users\nend/
155
+ end
118
156
  end
119
157
  end
@@ -1,12 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- # TODO: Move these specs to spec/actions_spec.rb
4
3
  describe TinyRails::Commands::Add do
5
4
  before do
6
5
  Dir.exist?('.tmp') ? FileUtils.rm_rf('.tmp/*') : Dir.mkdir('.tmp')
7
6
  @original_wd = Dir.pwd
8
7
  FileUtils.cd '.tmp'
9
- %w(.gitignore tiny_rails_controller.rb boot.rb Gemfile).each do |file|
8
+ %w(.gitignore application_controller.rb boot.rb Gemfile).each do |file|
10
9
  `touch #{file}`
11
10
  end
12
11
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe TinyRails::Commands::Console do
4
+ before do
5
+ Dir.exist?('.tmp') ? FileUtils.rm_rf('.tmp/*') : Dir.mkdir('.tmp')
6
+ @original_wd = Dir.pwd
7
+ FileUtils.cd '.tmp'
8
+ `touch boot.rb`
9
+
10
+ class ::TinyRailsApp; end
11
+ class ::Rails
12
+ class Console
13
+ end
14
+ end
15
+
16
+ Rails::Console.stub(:start)
17
+
18
+ command = described_class.new
19
+ @required_files = []
20
+ command.stub(:require) do |file|
21
+ @required_files << file
22
+ end
23
+
24
+ capture(:stdout) { command.invoke_all }
25
+ end
26
+
27
+ after { FileUtils.cd @original_wd }
28
+
29
+ it 'requires boot.rb file' do
30
+ @required_files.should include('./boot.rb')
31
+ end
32
+
33
+ it 'requires rails command' do
34
+ @required_files.should include('rails/commands/console')
35
+ end
36
+
37
+ it 'starts rails console' do
38
+ Rails::Console.should have_received(:start).with(TinyRailsApp)
39
+ end
40
+ end
@@ -18,7 +18,7 @@ describe TinyRails::Commands::New do
18
18
  .gitignore
19
19
  Gemfile
20
20
  boot.rb
21
- tiny_rails_controller.rb
21
+ application_controller.rb
22
22
  index.html.erb
23
23
  server
24
24
  config.ru
@@ -1,6 +1,8 @@
1
1
  require 'tiny-rails'
2
2
  require 'tiny-rails/cli'
3
3
 
4
+ require 'rspec-spies'
5
+
4
6
  Dir['./spec/support/**/*.rb'].each { |file| require file }
5
7
 
6
8
  RSpec.configure do |config|
@@ -3,5 +3,6 @@ source :rubygems
3
3
  # Required by active_support
4
4
  gem "tzinfo"
5
5
 
6
+ gem "tiny-rails"
6
7
  gem "actionpack", "~> 3.2"
7
8
  gem "railties", "~> 3.2"
@@ -8,21 +8,21 @@ def confirm_drop_db?
8
8
  answer.blank? || answer =~ /^y/
9
9
  end
10
10
 
11
- exit 0 unless confirm_drop_db?
12
-
13
11
  config = TinyRailsApp.config.database_configuration[Rails.env]
14
12
 
15
- # DROP DATABASE
16
13
  require 'pathname'
17
14
  path = Pathname.new(config['database'])
18
15
  file = path.absolute? ? path.to_s : File.join(Rails.root, path)
19
- FileUtils.rm(file)
20
16
 
21
- # CREATE DATABASE
17
+ exit 0 if File.exist?(file) && !confirm_drop_db?
18
+
19
+ # "Drop" database
20
+ FileUtils.rm(file) if File.exist?(file)
21
+
22
+ # Creates database
22
23
  ActiveRecord::Base.establish_connection(config)
23
24
  ActiveRecord::Base.connection
24
25
 
25
-
26
26
  ActiveRecord::Schema.define do
27
27
  create_table "posts" do |t|
28
28
  t.string "title"
@@ -1,4 +1,4 @@
1
- class TinyRailsController < ActionController::Base
1
+ class ApplicationController < ActionController::Base
2
2
  append_view_path File.dirname(__FILE__)
3
3
 
4
4
  def index
@@ -3,17 +3,14 @@ $:.unshift Dir.pwd
3
3
  require 'bundler'
4
4
  Bundler.setup :default
5
5
 
6
+ require "tiny-rails"
6
7
  require "rails"
7
- require "rails/all"
8
+
9
+ require "action_controller/railtie"
8
10
 
9
11
  Bundler.require :default
10
12
 
11
13
  class TinyRailsApp < Rails::Application
12
- routes.append do
13
- match "/" => "tiny_rails#index"
14
- match "/favicon.ico", :to => proc {|env| [200, {}, [""]] }
15
- end
16
-
17
14
  config.consider_all_requests_local = true
18
15
 
19
16
  config.active_support.deprecation = :log
@@ -29,6 +26,11 @@ class TinyRailsApp < Rails::Application
29
26
  config.secret_token = "49837489qkuweoiuoqwehisuakshdjksadhaisdy78o34y138974xyqp9rmye8yrpiokeuioqwzyoiuxftoyqiuxrhm3iou1hrzmjk"
30
27
  end
31
28
 
29
+ require 'initializers' if File.exists?('initializers.rb')
30
+
32
31
  TinyRailsApp.initialize!
33
32
 
34
- require 'initializers' if File.exists?('initializers.rb')
33
+ TinyRailsApp.routes.draw do
34
+ match "/" => "application#index"
35
+ match "/favicon.ico", :to => proc {|env| [200, {}, [""]] }
36
+ end
@@ -2,15 +2,6 @@
2
2
 
3
3
  require './boot'
4
4
 
5
- if ARGV[0] == '-v' || ARGV[0] == '--verbose'
6
- puts ">> Starting Rails lightweight stack"
7
- Rails.configuration.middleware.each do |middleware|
8
- puts "use #{middleware.inspect}"
9
- end
10
- puts "run #{Rails.application.class.name}.routes"
11
- end
12
-
13
-
14
5
  options = {
15
6
  :environment => nil,
16
7
  :pid => nil,
@@ -22,5 +22,6 @@ Gem::Specification.new do |gem|
22
22
  gem.add_development_dependency 'bundler', '~> 1.0'
23
23
  gem.add_development_dependency 'rake', '~> 0.9'
24
24
  gem.add_development_dependency 'rspec', '~> 2.0'
25
+ gem.add_development_dependency 'rspec-spies', '~> 2.1'
25
26
  gem.add_development_dependency 'guard-rspec', '~> 2.0'
26
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-11 00:00:00.000000000 Z
12
+ date: 2012-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -81,6 +81,22 @@ dependencies:
81
81
  - - ~>
82
82
  - !ruby/object:Gem::Version
83
83
  version: '2.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec-spies
86
+ requirement: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: '2.1'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: '2.1'
84
100
  - !ruby/object:Gem::Dependency
85
101
  name: guard-rspec
86
102
  requirement: !ruby/object:Gem::Requirement
@@ -119,12 +135,15 @@ files:
119
135
  - bin/tiny-rails
120
136
  - lib/tiny-rails.rb
121
137
  - lib/tiny-rails/actions.rb
138
+ - lib/tiny-rails/app/initializers/client_side_validations.rb
122
139
  - lib/tiny-rails/cli.rb
123
140
  - lib/tiny-rails/commands/add.rb
141
+ - lib/tiny-rails/commands/console.rb
124
142
  - lib/tiny-rails/commands/new.rb
125
143
  - lib/tiny-rails/version.rb
126
144
  - spec/actions_spec.rb
127
145
  - spec/commands/add_spec.rb
146
+ - spec/commands/console_spec.rb
128
147
  - spec/commands/new_spec.rb
129
148
  - spec/fixtures/sample_addon_1.rb
130
149
  - spec/fixtures/sample_addon_2.rb
@@ -134,11 +153,11 @@ files:
134
153
  - templates/Gemfile
135
154
  - templates/activerecord/migrate
136
155
  - templates/activerecord/models.rb
156
+ - templates/application_controller.rb
137
157
  - templates/boot.rb
138
158
  - templates/config.ru
139
159
  - templates/index.html.erb
140
160
  - templates/server
141
- - templates/tiny_rails_controller.rb
142
161
  - tiny-rails.gemspec
143
162
  homepage: http://github.com/fgrehm/tiny-rails
144
163
  licenses: []
@@ -154,7 +173,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
173
  version: '0'
155
174
  segments:
156
175
  - 0
157
- hash: -1156523687678209514
176
+ hash: -4104238879239819067
158
177
  required_rubygems_version: !ruby/object:Gem::Requirement
159
178
  none: false
160
179
  requirements:
@@ -163,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
182
  version: '0'
164
183
  segments:
165
184
  - 0
166
- hash: -1156523687678209514
185
+ hash: -4104238879239819067
167
186
  requirements: []
168
187
  rubyforge_project:
169
188
  rubygems_version: 1.8.23
@@ -173,6 +192,7 @@ summary: A generator for tiny Rails apps
173
192
  test_files:
174
193
  - spec/actions_spec.rb
175
194
  - spec/commands/add_spec.rb
195
+ - spec/commands/console_spec.rb
176
196
  - spec/commands/new_spec.rb
177
197
  - spec/fixtures/sample_addon_1.rb
178
198
  - spec/fixtures/sample_addon_2.rb