miniblog 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +5 -0
  3. data/.gitignore +17 -0
  4. data/.rspec +2 -0
  5. data/.simplecov +13 -0
  6. data/.travis.yml +17 -0
  7. data/CHANGELOG.md +7 -0
  8. data/Gemfile +20 -0
  9. data/Gemfile.4.1 +19 -0
  10. data/MIT-LICENSE +20 -0
  11. data/README.md +86 -0
  12. data/Rakefile +29 -0
  13. data/app/assets/images/crowdblog/.gitkeep +0 -0
  14. data/app/assets/javascripts/miniblog.js +7 -0
  15. data/app/assets/javascripts/miniblog/alerts.js +5 -0
  16. data/app/assets/javascripts/miniblog/form-errors.js +3 -0
  17. data/app/assets/stylesheets/application.css +3 -0
  18. data/app/assets/stylesheets/miniblog.css +5 -0
  19. data/app/assets/stylesheets/miniblog/bootstrap_and_overrides.css.scss +1 -0
  20. data/app/assets/stylesheets/miniblog/posts.css.scss +34 -0
  21. data/app/controllers/miniblog/admin/assets_controller.rb +20 -0
  22. data/app/controllers/miniblog/admin/base_controller.rb +7 -0
  23. data/app/controllers/miniblog/admin/posts_controller.rb +63 -0
  24. data/app/controllers/miniblog/admin/preview_controller.rb +8 -0
  25. data/app/controllers/miniblog/admin/states_controller.rb +11 -0
  26. data/app/controllers/miniblog/admin/transitions_controller.rb +22 -0
  27. data/app/controllers/miniblog/application_controller.rb +14 -0
  28. data/app/controllers/miniblog/posts_controller.rb +7 -0
  29. data/app/helpers/miniblog/admin/posts_helper.rb +18 -0
  30. data/app/helpers/miniblog/application_helper.rb +4 -0
  31. data/app/models/miniblog/asset.rb +7 -0
  32. data/app/models/miniblog/post.rb +135 -0
  33. data/app/models/miniblog/status_change_record.rb +6 -0
  34. data/app/models/miniblog/user.rb +32 -0
  35. data/app/presenters/miniblog/post_presenter.rb +13 -0
  36. data/app/uploaders/attachment_uploader.rb +48 -0
  37. data/app/views/layouts/miniblog/admin/base.html.erb +26 -0
  38. data/app/views/miniblog/admin/posts/_form.html.erb +10 -0
  39. data/app/views/miniblog/admin/posts/_form.html.slim +27 -0
  40. data/app/views/miniblog/admin/posts/_post.html.erb +14 -0
  41. data/app/views/miniblog/admin/posts/_post.html.slim +20 -0
  42. data/app/views/miniblog/admin/posts/edit.html.erb +19 -0
  43. data/app/views/miniblog/admin/posts/edit.html.slim +14 -0
  44. data/app/views/miniblog/admin/posts/index.html.erb +5 -0
  45. data/app/views/miniblog/admin/posts/new.html.erb +17 -0
  46. data/app/views/miniblog/admin/posts/new.html.slim +12 -0
  47. data/app/views/miniblog/application/_navbar.html.erb +19 -0
  48. data/app/views/miniblog/application/_notices.html.slim +6 -0
  49. data/app/views/miniblog/posts/_post.html.slim +4 -0
  50. data/app/views/miniblog/posts/index.html.slim +1 -0
  51. data/config/cucumber.yml +9 -0
  52. data/config/initializers/date_formats.rb +1 -0
  53. data/config/initializers/state_machine.rb +4 -0
  54. data/config/locales/devise.en.yml +57 -0
  55. data/config/routes.rb +26 -0
  56. data/db/migrate/20120217213920_create_miniblog_posts.rb +17 -0
  57. data/db/migrate/20120219071614_create_miniblog_assets.rb +10 -0
  58. data/lib/generators/miniblog/views_generator.rb +11 -0
  59. data/lib/miniblog.rb +18 -0
  60. data/lib/miniblog/devise/failure_app.rb +9 -0
  61. data/lib/miniblog/engine.rb +9 -0
  62. data/lib/miniblog/rspec.rb +1 -0
  63. data/lib/miniblog/rspec/miniblog_shared_examples.rb +77 -0
  64. data/lib/miniblog/version.rb +3 -0
  65. data/miniblog.gemspec +40 -0
  66. data/script/cucumber +10 -0
  67. data/script/rails +8 -0
  68. data/spec/dummy/README.rdoc +261 -0
  69. data/spec/dummy/Rakefile +7 -0
  70. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  71. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  72. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  73. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  74. data/spec/dummy/app/mailers/.gitkeep +0 -0
  75. data/spec/dummy/app/models/.gitkeep +0 -0
  76. data/spec/dummy/app/models/user.rb +3 -0
  77. data/spec/dummy/app/views/home/show.html.slim +4 -0
  78. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  79. data/spec/dummy/bin/bundle +3 -0
  80. data/spec/dummy/bin/rails +4 -0
  81. data/spec/dummy/bin/rake +4 -0
  82. data/spec/dummy/config.ru +4 -0
  83. data/spec/dummy/config/application.rb +28 -0
  84. data/spec/dummy/config/boot.rb +4 -0
  85. data/spec/dummy/config/database.yml +25 -0
  86. data/spec/dummy/config/environment.rb +5 -0
  87. data/spec/dummy/config/environments/development.rb +37 -0
  88. data/spec/dummy/config/environments/production.rb +82 -0
  89. data/spec/dummy/config/environments/test.rb +39 -0
  90. data/spec/dummy/config/initializers/assets.rb +8 -0
  91. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  92. data/spec/dummy/config/initializers/carrierwave.rb +18 -0
  93. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  94. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  95. data/spec/dummy/config/initializers/inflections.rb +16 -0
  96. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  97. data/spec/dummy/config/initializers/miniblog.rb +2 -0
  98. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  99. data/spec/dummy/config/initializers/session_store.rb +3 -0
  100. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  101. data/spec/dummy/config/locales/en.yml +23 -0
  102. data/spec/dummy/config/routes.rb +3 -0
  103. data/spec/dummy/config/secrets.yml +22 -0
  104. data/spec/dummy/db/schema.rb +50 -0
  105. data/spec/dummy/db/seed.rb +6 -0
  106. data/spec/dummy/lib/assets/.gitkeep +0 -0
  107. data/spec/dummy/lib/tasks/cucumber.rake +65 -0
  108. data/spec/dummy/public/404.html +26 -0
  109. data/spec/dummy/public/422.html +26 -0
  110. data/spec/dummy/public/500.html +25 -0
  111. data/spec/dummy/public/favicon.ico +0 -0
  112. data/spec/dummy/script/rails +6 -0
  113. data/spec/features/miniblog_spec.rb +5 -0
  114. data/spec/generators/miniblog/views_generator_spec.rb +16 -0
  115. data/spec/models/post_spec.rb +163 -0
  116. data/spec/spec_helper.rb +58 -0
  117. data/vendor/assets/javascripts/markdown.js +1470 -0
  118. data/vendor/assets/javascripts/uploader/jquery.html5uploader.js +148 -0
  119. metadata +464 -0
@@ -0,0 +1,14 @@
1
+ .span12
2
+ h2 Edit Post
3
+
4
+ = form_for [:admin, @post] do |f|
5
+ = render 'form', :f => f
6
+
7
+ .form-actions.clear
8
+ .row
9
+ .span1
10
+ = f.submit "Update", :class => "btn btn-primary"
11
+ .span1
12
+ = link_to 'Cancel', miniblog.admin_posts_path, :class => "btn"
13
+
14
+
@@ -0,0 +1,5 @@
1
+ <div class="page-header">
2
+ <h1>Posts</h1>
3
+ <%= link_to 'Click here for new post', new_admin_post_path %>
4
+ </div>
5
+ <%= render partial: 'miniblog/admin/posts/post', collection: @posts %>
@@ -0,0 +1,17 @@
1
+ <div class="page-header">
2
+ <h1>New Post</h1>
3
+ </div>
4
+ <%= form_for [:admin, @post] do |f| %>
5
+ <div class="row">
6
+ <%= render 'form', f: f %>
7
+ </div>
8
+ <div class="row well">
9
+ <div class="col-xs-4 col-xs-offset-1 col-sm-2 col-sm-offset-3">
10
+ <%= f.submit "Save", class: "btn btn-primary btn-block" %>
11
+ </div>
12
+ <div class="col-xs-4 col-xs-offset-2 col-sm-2">
13
+ <%= link_to 'Cancel', miniblog.admin_posts_path, class: "btn btn-default btn-block" %>
14
+ </div>
15
+ </div>
16
+ <% end %>
17
+
@@ -0,0 +1,12 @@
1
+ .span12
2
+ h2 New Post
3
+
4
+ = form_for @post do |f|
5
+ = render 'form', :f => f
6
+
7
+ .form-actions.clear
8
+ .row
9
+ .span1
10
+ = f.submit "Create", :class => "btn btn-primary"
11
+ .span1
12
+ = link_to 'Cancel', miniblog.posts_path, :class => "btn"
@@ -0,0 +1,19 @@
1
+ <nav class="navbar navbar-default">
2
+ <div class="container">
3
+ <div class="navbar-header">
4
+ <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-collapse">
5
+ <span class="sr-only">Toggle navigation</span>
6
+ <span class="icon-bar"></span>
7
+ <span class="icon-bar"></span>
8
+ <span class="icon-bar"></span>
9
+ </button>
10
+ <span class="navbar-brand">Miniblog</span>
11
+ </div>
12
+
13
+ <div class="collapse navbar-collapse" id="main-collapse">
14
+ <ul class="nav navbar-nav">
15
+ <li><%= link_to 'Posts', miniblog.admin_posts_path %></li>
16
+ </ul>
17
+ </div>
18
+ </div>
19
+ </nav>
@@ -0,0 +1,6 @@
1
+ - if flash[:notice]
2
+ .alert.alert-success
3
+ = flash[:notice]
4
+ - if flash[:alert]
5
+ .alert
6
+ = flash[:alert]
@@ -0,0 +1,4 @@
1
+ = div_for post, :class => "span12" do
2
+ h1= post.title
3
+ h2= post.author_email
4
+ .body= post.html_body
@@ -0,0 +1 @@
1
+ = render @posts
@@ -0,0 +1,9 @@
1
+ <%
2
+ require 'fuubar-cucumber'
3
+ rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
4
+ rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
5
+ std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'fuubar'} --color --strict --tags ~@wip -r features/"
6
+ %>
7
+ default: <%= std_opts %> features
8
+ wip: --color -r features/ --tags @wip:3 --wip features
9
+ rerun: <%= rerun_opts %> --color -r features/ --format rerun --out rerun.txt --strict --tags ~@wip
@@ -0,0 +1 @@
1
+ Time::DATE_FORMATS[:miniblog_short] = '%Y-%m-%d'
@@ -0,0 +1,4 @@
1
+ # https://github.com/pluginaweek/state_machine/issues/251
2
+ module StateMachine::Integrations::ActiveModel
3
+ public :around_validation
4
+ end
@@ -0,0 +1,57 @@
1
+ # Additional translations at https://github.com/plataformatec/devise/wiki/I18n
2
+
3
+ en:
4
+ errors:
5
+ messages:
6
+ expired: "has expired, please request a new one"
7
+ not_found: "not found"
8
+ already_confirmed: "was already confirmed, please try signing in"
9
+ not_locked: "was not locked"
10
+ not_saved:
11
+ one: "1 error prohibited this %{resource} from being saved:"
12
+ other: "%{count} errors prohibited this %{resource} from being saved:"
13
+
14
+ devise:
15
+ failure:
16
+ already_authenticated: 'You are already signed in.'
17
+ unauthenticated: 'You need to sign in or sign up before continuing.'
18
+ unconfirmed: 'You have to confirm your account before continuing.'
19
+ locked: 'Your account is locked.'
20
+ invalid: 'Invalid email or password.'
21
+ invalid_token: 'Invalid authentication token.'
22
+ timeout: 'Your session expired, please sign in again to continue.'
23
+ inactive: 'Your account was not activated yet.'
24
+ sessions:
25
+ signed_in: 'Signed in successfully.'
26
+ signed_out: 'Signed out successfully.'
27
+ passwords:
28
+ send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes.'
29
+ updated: 'Your password was changed successfully. You are now signed in.'
30
+ updated_not_active: 'Your password was changed successfully.'
31
+ send_paranoid_instructions: "If your e-mail exists on our database, you will receive a password recovery link on your e-mail"
32
+ confirmations:
33
+ send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.'
34
+ send_paranoid_instructions: 'If your e-mail exists on our database, you will receive an email with instructions about how to confirm your account in a few minutes.'
35
+ confirmed: 'Your account was successfully confirmed. You are now signed in.'
36
+ registrations:
37
+ signed_up: 'Welcome! You have signed up successfully.'
38
+ signed_up_but_unconfirmed: 'A message with a confirmation link has been sent to your email address. Please open the link to activate your account.'
39
+ signed_up_but_inactive: 'You have signed up successfully. However, we could not sign you in because your account is not yet activated.'
40
+ signed_up_but_locked: 'You have signed up successfully. However, we could not sign you in because your account is locked.'
41
+ updated: 'You updated your account successfully.'
42
+ update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address."
43
+ destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
44
+ unlocks:
45
+ send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
46
+ unlocked: 'Your account has been unlocked successfully. Please sign in to continue.'
47
+ send_paranoid_instructions: 'If your account exists, you will receive an email with instructions about how to unlock it in a few minutes.'
48
+ omniauth_callbacks:
49
+ success: 'Successfully authorized from %{kind} account.'
50
+ failure: 'Could not authorize you from %{kind} because "%{reason}".'
51
+ mailer:
52
+ confirmation_instructions:
53
+ subject: 'Confirmation instructions'
54
+ reset_password_instructions:
55
+ subject: 'Reset password instructions'
56
+ unlock_instructions:
57
+ subject: 'Unlock Instructions'
@@ -0,0 +1,26 @@
1
+ Miniblog::Engine.routes.draw do
2
+ root :to => 'posts#index'
3
+
4
+ namespace :admin do
5
+ resources :authors, only: :index
6
+
7
+ match 'posts/:state', :to => 'posts#index',
8
+ :constraints => { :state => /(published|drafted)/ },
9
+ :as => 'posts_by_state',
10
+ :via => :get
11
+
12
+ match 'posts/:id/:transition', :to => 'transitions#create',
13
+ :constraints => { :transition => /(draft|finish|review|publish)/ },
14
+ :as => 'post_transitions',
15
+ :via => :post
16
+
17
+ resources :posts do
18
+ resources :assets
19
+ resource :state, only: :update
20
+ end
21
+
22
+ resources :preview, only: :show
23
+
24
+ root :to => 'posts#index'
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ class CreateMiniblogPosts < ActiveRecord::Migration
2
+ def change
3
+ create_table :miniblog_posts do |t|
4
+ t.string :title
5
+ t.text :body
6
+ t.string :permalink
7
+ t.datetime :published_at
8
+ t.integer :author_id
9
+ t.string :state
10
+ t.integer :publisher_id
11
+ t.boolean :ready_for_review
12
+ t.datetime :marked_for_review_at
13
+
14
+ t.timestamps
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ class CreateMiniblogAssets < ActiveRecord::Migration
2
+ def change
3
+ create_table :miniblog_assets do |t|
4
+ t.integer :post_id
5
+ t.string :attachment
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails/generators'
2
+
3
+ module Miniblog
4
+ class ViewsGenerator < ::Rails::Generators::Base
5
+ source_root File.expand_path('../../../../app/', __FILE__)
6
+
7
+ def copy_views
8
+ directory 'views', 'app/views'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ require "miniblog/engine"
2
+
3
+ require "carrierwave"
4
+ require "gravtastic"
5
+ require "jbuilder"
6
+ require "redcarpet"
7
+ require "slim"
8
+ require "state_machine"
9
+
10
+ require "generators/miniblog/views_generator"
11
+
12
+ module Miniblog
13
+ @@author_user_class_name = 'User'
14
+ @@publisher_user_class_name = 'User'
15
+
16
+ mattr_accessor :author_user_class_name
17
+ mattr_accessor :publisher_user_class_name
18
+ end
@@ -0,0 +1,9 @@
1
+ module Miniblog
2
+ module Devise
3
+ class FailureApp < ::Devise::FailureApp
4
+ def redirect_url
5
+ miniblog.new_user_session_path
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Miniblog
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Miniblog
4
+
5
+ initializer :assets do |config|
6
+ Rails.application.config.assets.precompile += %w(miniblog.css miniblog.js)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'miniblog/rspec/miniblog_shared_examples'
@@ -0,0 +1,77 @@
1
+ shared_examples_for "a miniblog", :type => :feature do
2
+ let(:post) do
3
+ Miniblog::Post.create! :title => 'A post title', :body => 'A post body', :state => :drafted
4
+ end
5
+
6
+ describe "Home" do
7
+ it "shows published posts" do
8
+ post.save!
9
+ post.publish
10
+
11
+ visit root_path
12
+
13
+ within "#post_#{post.id}" do
14
+ expect(page).to have_content post.title
15
+ expect(page).to have_content post.body
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "Admin" do
21
+ describe "manage posts" do
22
+ it "creates a post", :js => true do
23
+ visit admin_posts_path
24
+ click_link 'Click here for new post'
25
+
26
+ fill_in 'Title', :with => 'A post title'
27
+ fill_in 'Body' , :with => 'A post body'
28
+ click_button 'Save'
29
+
30
+ post = Miniblog::Post.last
31
+ expect(page.current_path).to eq edit_admin_post_path(post)
32
+ end
33
+
34
+ context "a post exists" do
35
+ before do
36
+ post
37
+ post.save!
38
+ end
39
+
40
+ it "edits a post" do
41
+ visit admin_posts_path
42
+ within "#post_#{post.id}" do
43
+ click_link 'Edit', match: :first
44
+ end
45
+
46
+ fill_in 'Title', :with => 'A NEW post title'
47
+ fill_in 'Body' , :with => 'A NEW post body'
48
+ click_button 'Update'
49
+
50
+ expect(page.current_path).to eq admin_post_path(post)
51
+ expect(post.reload.title).to eq 'A NEW post title'
52
+ end
53
+
54
+ it "deletes a post" do
55
+ visit admin_posts_path
56
+
57
+ within "#post_#{post.id}" do
58
+ click_link 'Delete', match: :first
59
+ end
60
+ expect(page.current_path).to eq admin_posts_path
61
+ expect(page).to_not have_content 'A post title'
62
+ end
63
+
64
+ it "publishes a post", :js => true do
65
+ visit admin_posts_path
66
+
67
+ within "#post_#{post.id}" do
68
+ button = find_link 'Publish'
69
+ button.click
70
+
71
+ expect(post.reload.state).to eq 'published'
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module Miniblog
2
+ VERSION = '1.0.0.beta'
3
+ end
@@ -0,0 +1,40 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'miniblog/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'miniblog'
7
+ s.version = Miniblog::VERSION
8
+ s.authors = ['David Padilla']
9
+ s.email = %w(david@padilla.cc)
10
+ s.homepage = 'http://github.com/dabit/miniblog'
11
+ s.summary = 'miniblog base functionality and backend'
12
+ s.description = 'This mountable engine has the basic functionality to manage a very simple blog'
13
+ s.license = 'MIT'
14
+
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.require_paths = %w(lib app)
19
+
20
+ s.add_dependency 'rails', '~> 4.1'
21
+
22
+ s.add_dependency 'bootstrap-sass'
23
+ s.add_dependency 'carrierwave', '~> 0.10.0'
24
+ s.add_dependency 'gravtastic'
25
+ s.add_dependency 'jquery-rails'
26
+ s.add_dependency 'jbuilder'
27
+ s.add_dependency 'redcarpet', '~> 3.2.2'
28
+ s.add_dependency 'slim'
29
+ s.add_dependency 'state_machine', '~> 1.2.0'
30
+
31
+ s.add_development_dependency 'capybara-webkit', '~> 1.5.0'
32
+ s.add_development_dependency 'database_cleaner', '~> 1.4.1'
33
+ s.add_development_dependency 'fuubar'
34
+ s.add_development_dependency 'rspec-rails', '~> 3.2.1'
35
+ s.add_development_dependency 'rspec-activemodel-mocks'
36
+ s.add_development_dependency 'selenium-webdriver'
37
+ s.add_development_dependency 'simplecov', '~> 0.9.2'
38
+ s.add_development_dependency 'simplecov-rcov-text'
39
+ s.add_development_dependency 'sqlite3'
40
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ vendored_cucumber_bin = Dir["#{File.dirname(__FILE__)}/../vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
4
+ if vendored_cucumber_bin
5
+ load File.expand_path(vendored_cucumber_bin)
6
+ else
7
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
8
+ require 'cucumber'
9
+ load Cucumber::BINARY
10
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/miniblog/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | |-- images
161
+ | |-- javascripts
162
+ | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | `-- tasks
177
+ |-- log
178
+ |-- public
179
+ |-- script
180
+ |-- test
181
+ | |-- fixtures
182
+ | |-- functional
183
+ | |-- integration
184
+ | |-- performance
185
+ | `-- unit
186
+ |-- tmp
187
+ | |-- cache
188
+ | |-- pids
189
+ | |-- sessions
190
+ | `-- sockets
191
+ `-- vendor
192
+ |-- assets
193
+ `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.