effective_pages 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +30 -0
- data/Rakefile +24 -0
- data/app/assets/javascripts/effective_pages.js +0 -0
- data/app/assets/stylesheets/effective_pages/dropdown-submenu.css +7 -0
- data/app/assets/stylesheets/effective_pages.css.scss +1 -0
- data/app/controllers/admin/menus_controller.rb +79 -0
- data/app/controllers/admin/pages_controller.rb +95 -0
- data/app/controllers/effective/pages_controller.rb +24 -0
- data/app/helpers/effective_menus_helper.rb +151 -0
- data/app/helpers/effective_pages_helper.rb +14 -0
- data/app/models/effective/access_denied.rb +17 -0
- data/app/models/effective/datatables/menus.rb +16 -0
- data/app/models/effective/datatables/pages.rb +19 -0
- data/app/models/effective/menu.rb +152 -0
- data/app/models/effective/menu_item.rb +68 -0
- data/app/models/effective/page.rb +32 -0
- data/app/views/admin/menu_items/_actions.html.haml +4 -0
- data/app/views/admin/menu_items/_expand.html.haml +2 -0
- data/app/views/admin/menu_items/_item.html.haml +13 -0
- data/app/views/admin/menu_items/_new.html.haml +3 -0
- data/app/views/admin/menus/_actions.html.haml +4 -0
- data/app/views/admin/menus/_form.html.haml +9 -0
- data/app/views/admin/menus/edit.html.haml +2 -0
- data/app/views/admin/menus/index.html.haml +9 -0
- data/app/views/admin/menus/new.html.haml +2 -0
- data/app/views/admin/pages/_actions.html.haml +14 -0
- data/app/views/admin/pages/_form.html.haml +31 -0
- data/app/views/admin/pages/edit.html.haml +2 -0
- data/app/views/admin/pages/index.html.haml +9 -0
- data/app/views/admin/pages/new.html.haml +2 -0
- data/config/routes.rb +28 -0
- data/db/migrate/01_create_effective_pages.rb.erb +52 -0
- data/lib/effective_pages/engine.rb +19 -0
- data/lib/effective_pages/version.rb +3 -0
- data/lib/effective_pages.rb +73 -0
- data/lib/generators/effective_pages/install_generator.rb +43 -0
- data/lib/generators/templates/README +1 -0
- data/lib/generators/templates/effective_pages.rb +49 -0
- data/lib/generators/templates/example.html.haml +6 -0
- data/lib/tasks/effective_pages_tasks.rake +29 -0
- data/spec/controllers/effective/pages_controller_spec.rb +66 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/effective/pages/example.html.haml +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config/application.rb +66 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +60 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/01_create_effective_pages.rb +51 -0
- data/spec/dummy/db/schema.rb +52 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec_link +3 -0
- data/spec/effective_pages_spec.rb +7 -0
- data/spec/helpers/effective_menus_helper_spec.rb +281 -0
- data/spec/models/effective/menu_spec.rb +133 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/factories.rb +15 -0
- metadata +261 -0
data/config/routes.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
class EffectivePagesRoutingConstraint
|
2
|
+
def self.matches?(request)
|
3
|
+
id = request.path_parameters[:id] || '/'
|
4
|
+
Effective::Page.find(id).present? rescue false
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
EffectivePages::Engine.routes.draw do
|
9
|
+
if defined?(EffectiveDatatables)
|
10
|
+
namespace :admin do
|
11
|
+
resources :pages, :except => [:show]
|
12
|
+
resources :menus, :except => [:show]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
scope :module => 'effective' do
|
17
|
+
get '*id' => "pages#show", :constraints => EffectivePagesRoutingConstraint, :as => :page
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Automatically mount the engine as an append
|
22
|
+
Rails.application.routes.append do
|
23
|
+
unless Rails.application.routes.routes.find { |r| r.name == 'effective_pages' }
|
24
|
+
mount EffectivePages::Engine => '/', :as => 'effective_pages'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
#root :to => 'Effective::Pages#show', :id => 'home'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class CreateEffectivePages < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table <%= @pages_table_name %> do |t|
|
4
|
+
t.string :title
|
5
|
+
t.string :meta_description
|
6
|
+
|
7
|
+
t.boolean :draft, :default => false
|
8
|
+
|
9
|
+
t.string :layout, :default => 'application'
|
10
|
+
t.string :template
|
11
|
+
|
12
|
+
t.string :slug
|
13
|
+
t.integer :roles_mask, :default => 0
|
14
|
+
|
15
|
+
t.datetime :updated_at
|
16
|
+
t.datetime :created_at
|
17
|
+
end
|
18
|
+
add_index <%= @pages_table_name %>, :slug, :unique => true
|
19
|
+
|
20
|
+
create_table <%= @menus_table_name %> do |t|
|
21
|
+
t.string :title
|
22
|
+
t.timestamps
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table <%= @menu_items_table_name %> do |t|
|
26
|
+
t.integer :menu_id
|
27
|
+
|
28
|
+
t.integer :menuable_id
|
29
|
+
t.string :menuable_type
|
30
|
+
|
31
|
+
t.string :title
|
32
|
+
|
33
|
+
t.string :url
|
34
|
+
t.string :special
|
35
|
+
|
36
|
+
t.string :classes
|
37
|
+
t.boolean :new_window, :default => false
|
38
|
+
t.integer :roles_mask, :default => nil
|
39
|
+
|
40
|
+
t.integer :lft
|
41
|
+
t.integer :rgt
|
42
|
+
end
|
43
|
+
add_index <%= @menu_items_table_name %>, :lft
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.down
|
47
|
+
drop_table <%= @pages_table_name %>
|
48
|
+
drop_table <%= @menus_table_name %>
|
49
|
+
drop_table <%= @menu_items_table_name %>
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module EffectivePages
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
engine_name 'effective_pages'
|
4
|
+
|
5
|
+
# Include Helpers to base application
|
6
|
+
initializer 'effective_pages.action_controller' do |app|
|
7
|
+
ActiveSupport.on_load :action_controller do
|
8
|
+
helper EffectivePagesHelper
|
9
|
+
helper EffectiveMenusHelper
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Set up our default configuration options.
|
14
|
+
initializer "effective_pages.defaults", :before => :load_config_initializers do |app|
|
15
|
+
# Set up our defaults, as per our initializer template
|
16
|
+
eval File.read("#{config.root}/lib/generators/templates/effective_pages.rb")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require "effective_pages/engine"
|
2
|
+
require 'migrant' # Required for rspec to run properly
|
3
|
+
|
4
|
+
module EffectivePages
|
5
|
+
mattr_accessor :pages_table_name
|
6
|
+
mattr_accessor :menus_table_name
|
7
|
+
mattr_accessor :menu_items_table_name
|
8
|
+
|
9
|
+
mattr_accessor :pages_path
|
10
|
+
mattr_accessor :excluded_pages
|
11
|
+
mattr_accessor :excluded_layouts
|
12
|
+
|
13
|
+
mattr_accessor :authorization_method
|
14
|
+
mattr_accessor :simple_form_options
|
15
|
+
mattr_accessor :layout
|
16
|
+
mattr_accessor :menu
|
17
|
+
|
18
|
+
def self.setup
|
19
|
+
yield self
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.authorized?(controller, action, resource)
|
23
|
+
if authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol)
|
24
|
+
raise Effective::AccessDenied.new() unless (controller || self).instance_exec(controller, action, resource, &authorization_method)
|
25
|
+
end
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.pages
|
30
|
+
Rails.env.development? ? read_pages : (@@pages ||= read_pages)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.layouts
|
34
|
+
Rails.env.development? ? read_layouts : (@@layouts ||= read_layouts)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Remove leading and trailing '/' characters
|
38
|
+
# Will return: "effective/pages"
|
39
|
+
def self.pages_path=(filepath)
|
40
|
+
filepath = filepath.to_s
|
41
|
+
filepath = filepath[1..-1] if filepath.starts_with?('/')
|
42
|
+
@@pages_path = filepath.chomp('/')
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def self.read_pages
|
48
|
+
files = ApplicationController.view_paths.map { |path| Dir["#{path}/#{pages_path}/**"] }.flatten.reverse
|
49
|
+
|
50
|
+
HashWithIndifferentAccess.new().tap do |pages|
|
51
|
+
files.each do |file|
|
52
|
+
name = File.basename(file).split('.').first
|
53
|
+
next if name.starts_with?('_') || Array(EffectivePages.excluded_pages).map(&:to_s).include?(name)
|
54
|
+
|
55
|
+
pages[name.to_sym] = {}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.read_layouts
|
61
|
+
files = ApplicationController.view_paths.map { |path| Dir["#{path}/layouts/**"] }.flatten.reverse
|
62
|
+
|
63
|
+
HashWithIndifferentAccess.new().tap do |layouts|
|
64
|
+
files.each do |file|
|
65
|
+
name = File.basename(file).split('.').first
|
66
|
+
next if name.starts_with?('_') || Array(EffectivePages.excluded_layouts).map(&:to_s).include?(name)
|
67
|
+
|
68
|
+
layouts[name.to_sym] = {}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module EffectivePages
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
desc "Creates an EffectivePages initializer in your application."
|
7
|
+
|
8
|
+
source_root File.expand_path("../../templates", __FILE__)
|
9
|
+
|
10
|
+
def self.next_migration_number(dirname)
|
11
|
+
if not ActiveRecord::Base.timestamped_migrations
|
12
|
+
Time.new.utc.strftime("%Y%m%d%H%M%S")
|
13
|
+
else
|
14
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_initializer
|
19
|
+
template "effective_pages.rb", "config/initializers/effective_pages.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_migration_file
|
23
|
+
@pages_table_name = ':' + EffectivePages.pages_table_name.to_s
|
24
|
+
@menus_table_name = ':' + EffectivePages.menus_table_name.to_s
|
25
|
+
@menu_items_table_name = ':' + EffectivePages.menu_items_table_name.to_s
|
26
|
+
|
27
|
+
migration_template '../../../db/migrate/01_create_effective_pages.rb.erb', 'db/migrate/create_effective_pages.rb'
|
28
|
+
end
|
29
|
+
|
30
|
+
def copy_example_page
|
31
|
+
template 'example.html.haml', 'app/views/effective/pages/example.html.haml'
|
32
|
+
end
|
33
|
+
|
34
|
+
def setup_routes
|
35
|
+
inject_into_file "config/routes.rb", "\n # if you want EffectivePages to render the home / root page\n # uncomment the following line and create an Effective::Page with slug == 'home' \n # root :to => 'Effective::Pages#show', :id => 'home'\n", :before => /root (:?)to.*/
|
36
|
+
end
|
37
|
+
|
38
|
+
def show_readme
|
39
|
+
readme "README" if behavior == :invoke
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Thanks for using EffectivePages
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# EffectivePages Rails Engine
|
2
|
+
|
3
|
+
EffectivePages.setup do |config|
|
4
|
+
config.pages_table_name = :pages
|
5
|
+
config.menus_table_name = :menus
|
6
|
+
config.menu_items_table_name = :menu_items
|
7
|
+
|
8
|
+
# Relative to app/views/
|
9
|
+
config.pages_path = '/effective/pages/'
|
10
|
+
|
11
|
+
# Excluded Pages
|
12
|
+
config.excluded_pages = []
|
13
|
+
|
14
|
+
# Excluded Layouts
|
15
|
+
config.excluded_layouts = [:admin]
|
16
|
+
|
17
|
+
# Use CanCan: can?(action, resource)
|
18
|
+
# Use effective_roles: resource.roles_match_with?(current_user)
|
19
|
+
config.authorization_method = Proc.new { |controller, action, resource| true }
|
20
|
+
|
21
|
+
# Layout Settings
|
22
|
+
# Configure the Layout per controller, or all at once
|
23
|
+
|
24
|
+
# config.layout = 'application' # The layout for the EffectivePages admin screen
|
25
|
+
config.layout = {
|
26
|
+
:admin => 'application'
|
27
|
+
}
|
28
|
+
|
29
|
+
# SimpleForm Options
|
30
|
+
# This Hash of options will be passed into any simple_form_for() calls
|
31
|
+
config.simple_form_options = {}
|
32
|
+
|
33
|
+
# config.simple_form_options = {
|
34
|
+
# :html => {:class => 'form-horizontal'},
|
35
|
+
# :wrapper => :horizontal_form,
|
36
|
+
# :wrapper_mappings => {
|
37
|
+
# :boolean => :horizontal_boolean,
|
38
|
+
# :check_boxes => :horizontal_radio_and_checkboxes,
|
39
|
+
# :radio_buttons => :horizontal_radio_and_checkboxes
|
40
|
+
# }
|
41
|
+
# }
|
42
|
+
|
43
|
+
# All effective_page menu options
|
44
|
+
config.menu = {
|
45
|
+
:apply_active_class => true,
|
46
|
+
:maxdepth => 2
|
47
|
+
}
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# bundle exec rake effective_pages:seed
|
2
|
+
namespace :effective_pages do
|
3
|
+
task :seed => :environment do
|
4
|
+
include EffectiveMenusHelper
|
5
|
+
|
6
|
+
return true if Effective::Menu.find_by_title('main menu').present?
|
7
|
+
|
8
|
+
Effective::Menu.new(:title => 'main menu').build do
|
9
|
+
dropdown 'About' do
|
10
|
+
item '111'
|
11
|
+
item '222'
|
12
|
+
dropdown 'More...' do
|
13
|
+
item '333'
|
14
|
+
item '444'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
dropdown 'Pages' do
|
19
|
+
item 'AAA'
|
20
|
+
divider
|
21
|
+
item 'BBB'
|
22
|
+
item 'CCC'
|
23
|
+
divider
|
24
|
+
item 'DDD'
|
25
|
+
end
|
26
|
+
end.save!
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Effective::PagesController do
|
4
|
+
routes { EffectivePages::Engine.routes }
|
5
|
+
|
6
|
+
let(:page) { FactoryGirl.create(:page) }
|
7
|
+
let(:draft) { FactoryGirl.create(:page, :draft => true) }
|
8
|
+
let(:page_with_invalid_template) { FactoryGirl.create(:page, :template => 'notemplate') }
|
9
|
+
let(:page_with_invalid_layout) { FactoryGirl.create(:page, :layout => 'nolayout') }
|
10
|
+
|
11
|
+
before (:each) do
|
12
|
+
end
|
13
|
+
|
14
|
+
context "#show" do
|
15
|
+
context 'valid requests' do
|
16
|
+
it 'raises RecordNotFound when the page is a draft' do
|
17
|
+
expect {
|
18
|
+
get :show, :id => draft.id
|
19
|
+
}.to raise_error(ActiveRecord::RecordNotFound)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'renders a draft page when edit=true is passed' do
|
23
|
+
get :show, :id => draft.id, :edit => true
|
24
|
+
|
25
|
+
response.should be_success
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'renders the appropriate page template' do
|
29
|
+
get :show, :id => page.id
|
30
|
+
|
31
|
+
expect(response).to render_template('layouts/application')
|
32
|
+
expect(response).to render_template('effective/pages/example')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'invalid requests' do
|
37
|
+
it 'raises RecordNotFound when passed invalid ID' do
|
38
|
+
expect { get :show, :id => 999 }.to raise_error(ActiveRecord::RecordNotFound)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'raises MissingTemplate when the page has an unknown template' do
|
42
|
+
expect {
|
43
|
+
get :show, :id => page_with_invalid_template.id
|
44
|
+
}.to raise_error(ActionView::MissingTemplate)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'raises MissingTemplate when the page has an unknown layout' do
|
48
|
+
expect {
|
49
|
+
get :show, :id => page_with_invalid_layout.id
|
50
|
+
}.to raise_error(ActionView::MissingTemplate)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'authentication' do
|
55
|
+
it 'prevents the page from being viewed when not authorized' do
|
56
|
+
original_auth_method = EffectivePages.authorization_method
|
57
|
+
EffectivePages.authorization_method = Proc.new { |controller, action, resource| false }
|
58
|
+
|
59
|
+
expect { get :show, :id => page.id }.to raise_error(Effective::AccessDenied)
|
60
|
+
|
61
|
+
EffectivePages.authorization_method = original_auth_method
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end # /show
|
65
|
+
|
66
|
+
end
|
@@ -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
|
+
| |-- assets
|
177
|
+
| `-- tasks
|
178
|
+
|-- log
|
179
|
+
|-- public
|
180
|
+
|-- script
|
181
|
+
|-- test
|
182
|
+
| |-- fixtures
|
183
|
+
| |-- functional
|
184
|
+
| |-- integration
|
185
|
+
| |-- performance
|
186
|
+
| `-- unit
|
187
|
+
|-- tmp
|
188
|
+
| `-- cache
|
189
|
+
| `-- assets
|
190
|
+
`-- vendor
|
191
|
+
|-- assets
|
192
|
+
| |-- javascripts
|
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.
|