rollable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENCE +19 -0
- data/README.textile +67 -0
- data/Rakefile +2 -0
- data/lib/generators/rollable/setup/setup_generator.rb +55 -0
- data/lib/generators/rollable/setup/templates/migration.rb +15 -0
- data/lib/generators/rollable/setup/templates/role.rb +4 -0
- data/lib/rollable.rb +3 -0
- data/lib/rollable/base.rb +92 -0
- data/lib/rollable/railtie.rb +7 -0
- data/lib/rollable/version.rb +3 -0
- data/rollable.gemspec +23 -0
- data/spec/rollable_test/.gitignore +4 -0
- data/spec/rollable_test/.rspec +1 -0
- data/spec/rollable_test/Gemfile +32 -0
- data/spec/rollable_test/README +256 -0
- data/spec/rollable_test/Rakefile +7 -0
- data/spec/rollable_test/app/controllers/application_controller.rb +3 -0
- data/spec/rollable_test/app/helpers/application_helper.rb +2 -0
- data/spec/rollable_test/app/models/horse.rb +3 -0
- data/spec/rollable_test/app/models/pig.rb +3 -0
- data/spec/rollable_test/app/models/role.rb +4 -0
- data/spec/rollable_test/app/models/user.rb +6 -0
- data/spec/rollable_test/app/views/layouts/application.html.erb +14 -0
- data/spec/rollable_test/config.ru +4 -0
- data/spec/rollable_test/config/application.rb +42 -0
- data/spec/rollable_test/config/boot.rb +6 -0
- data/spec/rollable_test/config/database.yml +22 -0
- data/spec/rollable_test/config/environment.rb +5 -0
- data/spec/rollable_test/config/environments/development.rb +26 -0
- data/spec/rollable_test/config/environments/production.rb +49 -0
- data/spec/rollable_test/config/environments/test.rb +35 -0
- data/spec/rollable_test/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rollable_test/config/initializers/inflections.rb +10 -0
- data/spec/rollable_test/config/initializers/mime_types.rb +5 -0
- data/spec/rollable_test/config/initializers/secret_token.rb +7 -0
- data/spec/rollable_test/config/initializers/session_store.rb +8 -0
- data/spec/rollable_test/config/locales/en.yml +5 -0
- data/spec/rollable_test/config/routes.rb +58 -0
- data/spec/rollable_test/db/migrate/20110421101457_create_users.rb +12 -0
- data/spec/rollable_test/db/migrate/20110421101514_create_horses.rb +12 -0
- data/spec/rollable_test/db/migrate/20110421101523_create_pigs.rb +12 -0
- data/spec/rollable_test/db/migrate/20110423152854_create_roles.rb +15 -0
- data/spec/rollable_test/db/schema.rb +39 -0
- data/spec/rollable_test/db/seeds.rb +7 -0
- data/spec/rollable_test/doc/README_FOR_APP +2 -0
- data/spec/rollable_test/lib/tasks/.gitkeep +0 -0
- data/spec/rollable_test/public/404.html +26 -0
- data/spec/rollable_test/public/422.html +26 -0
- data/spec/rollable_test/public/500.html +26 -0
- data/spec/rollable_test/public/favicon.ico +0 -0
- data/spec/rollable_test/public/images/rails.png +0 -0
- data/spec/rollable_test/public/index.html +239 -0
- data/spec/rollable_test/public/javascripts/application.js +2 -0
- data/spec/rollable_test/public/javascripts/controls.js +965 -0
- data/spec/rollable_test/public/javascripts/dragdrop.js +974 -0
- data/spec/rollable_test/public/javascripts/effects.js +1123 -0
- data/spec/rollable_test/public/javascripts/prototype.js +6001 -0
- data/spec/rollable_test/public/javascripts/rails.js +191 -0
- data/spec/rollable_test/public/robots.txt +5 -0
- data/spec/rollable_test/public/stylesheets/.gitkeep +0 -0
- data/spec/rollable_test/script/rails +6 -0
- data/spec/rollable_test/spec/models/horse_spec.rb +5 -0
- data/spec/rollable_test/spec/models/pig_spec.rb +5 -0
- data/spec/rollable_test/spec/models/role_spec.rb +26 -0
- data/spec/rollable_test/spec/models/user_spec.rb +23 -0
- data/spec/rollable_test/spec/spec_helper.rb +27 -0
- data/spec/rollable_test/test/performance/browsing_test.rb +9 -0
- data/spec/rollable_test/test/test_helper.rb +13 -0
- data/spec/rollable_test/vendor/plugins/.gitkeep +0 -0
- data/spec/spec_helper.rb +28 -0
- metadata +147 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENCE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Timon Vonk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
h1. Rollable
|
2
|
+
|
3
|
+
Rollable is a Rails gem that adds agnostic roles to your application. The core principle is that a role should not
|
4
|
+
be coupled to any object, but is nothing more than a relationship. Rollable does not define semantics, you can use
|
5
|
+
cancan or some similar gem to do just that.
|
6
|
+
|
7
|
+
h2. Installation
|
8
|
+
|
9
|
+
Add to your gemfile
|
10
|
+
|
11
|
+
bq. gem 'rollable'
|
12
|
+
|
13
|
+
And run
|
14
|
+
|
15
|
+
bq. bundle install
|
16
|
+
|
17
|
+
h2. Setup
|
18
|
+
|
19
|
+
Rollable is ment to be easy and maintainable, with a minimal amount of configuration nescecary.
|
20
|
+
|
21
|
+
Setup the role model and migration, where <user> is the model name you want to be able to have roles
|
22
|
+
|
23
|
+
bq. rails generate rollable:setup
|
24
|
+
rake db:migrate
|
25
|
+
|
26
|
+
h2. Configuration
|
27
|
+
|
28
|
+
Just two lines!
|
29
|
+
|
30
|
+
In the model you defined during setup:
|
31
|
+
|
32
|
+
bq. include Rollable::Base
|
33
|
+
rollables model names, option hash
|
34
|
+
|
35
|
+
Currently, the available options are:
|
36
|
+
|
37
|
+
bq. :roles => [Array of Strings] # Role names
|
38
|
+
:allow_nil => false # If true, you can create generic roles (like 'admin')
|
39
|
+
|
40
|
+
h2. Features
|
41
|
+
|
42
|
+
* Hardly any configuration
|
43
|
+
* Validates role names, role types by inclusion and user presence on Role (all dynamic)
|
44
|
+
* Much more!
|
45
|
+
|
46
|
+
h2. Usage
|
47
|
+
|
48
|
+
Rollable provides helpers based on role names to check whether a user has a role on an object:
|
49
|
+
|
50
|
+
bq. user.is_owner_of?(horse) # => true
|
51
|
+
|
52
|
+
You can also request all possible roles
|
53
|
+
|
54
|
+
bq. User.role_names
|
55
|
+
|
56
|
+
Lastly, if you use nil roles, you can also do
|
57
|
+
|
58
|
+
bq. user.is_admin?
|
59
|
+
|
60
|
+
h2. Limitations
|
61
|
+
|
62
|
+
* Its the first release, don't whine.
|
63
|
+
* Role names can only be single words (so no underscores), but keep it simple, right?
|
64
|
+
|
65
|
+
Questions? Comments? Contribute? All welcome!
|
66
|
+
|
67
|
+
- Timon Vonk
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module Rollable
|
5
|
+
module Generators
|
6
|
+
class SetupGenerator < Rails::Generators::NamedBase
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
desc "Sets up rollable, creates model and migration"
|
10
|
+
source_root(File.expand_path('../templates', __FILE__))
|
11
|
+
|
12
|
+
def copy_role_model_template
|
13
|
+
template 'role.rb', 'app/models/role.rb'
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_role_migration_template
|
17
|
+
migration_template 'migration.rb', "db/migrate/create_roles.rb"
|
18
|
+
end
|
19
|
+
|
20
|
+
def some_notes
|
21
|
+
puts <<-END
|
22
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
23
|
+
Rollable has been succesfully installed!
|
24
|
+
|
25
|
+
Note that rollable is still in its early stages. Rollable does not
|
26
|
+
and will not ever define authorization semantics.
|
27
|
+
Its purely intended for agnostic roles between objects only.
|
28
|
+
|
29
|
+
If you like it, leave a note or feel free to contribute!
|
30
|
+
|
31
|
+
To use rollable, make sure you add:
|
32
|
+
|
33
|
+
include Rollable::Base
|
34
|
+
rollables <model names>, <options>
|
35
|
+
|
36
|
+
in #{class_name}!
|
37
|
+
|
38
|
+
Have fun,
|
39
|
+
Timon Vonk
|
40
|
+
END
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def self.next_migration_number(dirname)
|
45
|
+
if ActiveRecord::Base.timestamped_migrations
|
46
|
+
Time.new.utc.strftime("%Y%m%d%H%M%S")
|
47
|
+
else
|
48
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateRoles < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :roles do |t|
|
4
|
+
t.string :name
|
5
|
+
t.references :<%= plural_name.singularize %>
|
6
|
+
t.references :rollable, :polymorphic => true
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
drop_table :roles
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
data/lib/rollable.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
module Rollable
|
2
|
+
module Base
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:has_many, :roles)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def rollables(*models, opts)
|
10
|
+
# So we can access it in respond_to?
|
11
|
+
@rollables = models.collect { |m| m.to_s.camelize }#Accepting symbols looks nicer than constants, so we need to convert them
|
12
|
+
@role_names = opts[:roles] if opts.has_key?(:roles)
|
13
|
+
|
14
|
+
if opts.has_key?(:allow_nil) && opts[:allow_nil]
|
15
|
+
@rollables << nil
|
16
|
+
end
|
17
|
+
|
18
|
+
self.class_eval do
|
19
|
+
# Hackety hack.
|
20
|
+
class << self; attr_reader :rollables, :role_names; end;
|
21
|
+
end
|
22
|
+
|
23
|
+
set_relations_on_rollables
|
24
|
+
set_role_validations
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def set_role_validations
|
29
|
+
# Yugh, scope and self, ugly but it works.
|
30
|
+
rollables = @rollables
|
31
|
+
roles = @role_names
|
32
|
+
other = self
|
33
|
+
Role.class_eval do
|
34
|
+
validates_presence_of other.to_s.downcase + "_id"
|
35
|
+
validates_inclusion_of :rollable_type, :in => rollables # Programmers are stupid, so lets help them out.
|
36
|
+
if roles.any?
|
37
|
+
validates_inclusion_of :name, :in => roles
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def set_relations_on_rollables
|
43
|
+
@rollables.select { |r| r.is_a?(String) }.each do |model|
|
44
|
+
model = model.constantize
|
45
|
+
if model.ancestors.include?(ActiveRecord::Base) #Check if it is an actual rails model, just to be sure
|
46
|
+
model.class_eval do # Enter the given models class
|
47
|
+
has_many :roles, :as => 'rollable' # Define the relationship <img src="http://www.timonv.nl/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley">
|
48
|
+
end
|
49
|
+
else
|
50
|
+
raise "#{model} is not an ActiveRecord object!" #Again, just to be sure and nice.
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def method_missing(method, *args)
|
57
|
+
if method =~ /^is_([a-z]+)(?:_(?:on|of))?\?$/ #Common spell to match against regex
|
58
|
+
role = $1
|
59
|
+
that_thing = args.first.presence
|
60
|
+
object = that_thing.class.to_s if that_thing
|
61
|
+
self.class_eval do #Open my class definition
|
62
|
+
if that_thing
|
63
|
+
define_method(method) do |thing| # Define the method. Helps a lot in performance
|
64
|
+
self.roles.where("rollable_type = ? AND name = ?", object, role).inject(false) do |v,o| # You can't do an inner join on polymorphic relationships, unfortunately.
|
65
|
+
v ||= (o.rollable == thing)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
else
|
69
|
+
define_method(method) do
|
70
|
+
self.roles.where("name = ?", role).count > 0
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
if that_thing
|
75
|
+
self.public_send(method, that_thing) # And of course, call the method.
|
76
|
+
else
|
77
|
+
self.public_send(method)
|
78
|
+
end
|
79
|
+
else
|
80
|
+
super
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def respond_to?(method, include_private=false)
|
85
|
+
if method =~ /^is_([a-z]+)(?:_(?:on|of))?\?$/
|
86
|
+
self.class.role_names.include?($1)
|
87
|
+
else
|
88
|
+
super
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/rollable.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rollable/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rollable"
|
7
|
+
s.version = Rollable::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Timon Vonk"]
|
10
|
+
s.email = ["mail@timonv.nl"]
|
11
|
+
s.homepage = "http://www.timonv.nl"
|
12
|
+
s.summary = %q{Agnostic roles for rails}
|
13
|
+
s.description = %q{This gem adds agnostic roles for authorization to Rails.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "rollable"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_dependency "rails", ">= 3.0.0"
|
22
|
+
s.add_dependency "rspec"
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
@@ -0,0 +1,32 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.0.7'
|
4
|
+
|
5
|
+
# Bundle edge Rails instead:
|
6
|
+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
+
|
8
|
+
gem 'sqlite3'
|
9
|
+
gem 'rollable', :path => '../../'
|
10
|
+
|
11
|
+
# Use unicorn as the web server
|
12
|
+
# gem 'unicorn'
|
13
|
+
|
14
|
+
# Deploy with Capistrano
|
15
|
+
# gem 'capistrano'
|
16
|
+
|
17
|
+
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
|
18
|
+
# gem 'ruby-debug'
|
19
|
+
# gem 'ruby-debug19', :require => 'ruby-debug'
|
20
|
+
|
21
|
+
# Bundle the extra gems:
|
22
|
+
# gem 'bj'
|
23
|
+
# gem 'nokogiri'
|
24
|
+
# gem 'sqlite3-ruby', :require => 'sqlite3'
|
25
|
+
# gem 'aws-s3', :require => 'aws/s3'
|
26
|
+
|
27
|
+
# Bundle gems for the local environment. Make sure to
|
28
|
+
# put test-only gems in this group so their generators
|
29
|
+
# and rake tasks are available in development mode:
|
30
|
+
group :development, :test do
|
31
|
+
gem 'rspec-rails'
|
32
|
+
end
|
@@ -0,0 +1,256 @@
|
|
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.find(: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.com/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
|
+
| |-- controllers
|
160
|
+
| |-- helpers
|
161
|
+
| |-- mailers
|
162
|
+
| |-- models
|
163
|
+
| `-- views
|
164
|
+
| `-- layouts
|
165
|
+
|-- config
|
166
|
+
| |-- environments
|
167
|
+
| |-- initializers
|
168
|
+
| `-- locales
|
169
|
+
|-- db
|
170
|
+
|-- doc
|
171
|
+
|-- lib
|
172
|
+
| `-- tasks
|
173
|
+
|-- log
|
174
|
+
|-- public
|
175
|
+
| |-- images
|
176
|
+
| |-- javascripts
|
177
|
+
| `-- stylesheets
|
178
|
+
|-- script
|
179
|
+
|-- test
|
180
|
+
| |-- fixtures
|
181
|
+
| |-- functional
|
182
|
+
| |-- integration
|
183
|
+
| |-- performance
|
184
|
+
| `-- unit
|
185
|
+
|-- tmp
|
186
|
+
| |-- cache
|
187
|
+
| |-- pids
|
188
|
+
| |-- sessions
|
189
|
+
| `-- sockets
|
190
|
+
`-- vendor
|
191
|
+
`-- plugins
|
192
|
+
|
193
|
+
app
|
194
|
+
Holds all the code that's specific to this particular application.
|
195
|
+
|
196
|
+
app/controllers
|
197
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
198
|
+
automated URL mapping. All controllers should descend from
|
199
|
+
ApplicationController which itself descends from ActionController::Base.
|
200
|
+
|
201
|
+
app/models
|
202
|
+
Holds models that should be named like post.rb. Models descend from
|
203
|
+
ActiveRecord::Base by default.
|
204
|
+
|
205
|
+
app/views
|
206
|
+
Holds the template files for the view that should be named like
|
207
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
208
|
+
eRuby syntax by default.
|
209
|
+
|
210
|
+
app/views/layouts
|
211
|
+
Holds the template files for layouts to be used with views. This models the
|
212
|
+
common header/footer method of wrapping views. In your views, define a layout
|
213
|
+
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
214
|
+
Inside default.html.erb, call <% yield %> to render the view using this
|
215
|
+
layout.
|
216
|
+
|
217
|
+
app/helpers
|
218
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are
|
219
|
+
generated for you automatically when using generators for controllers.
|
220
|
+
Helpers can be used to wrap functionality for your views into methods.
|
221
|
+
|
222
|
+
config
|
223
|
+
Configuration files for the Rails environment, the routing map, the database,
|
224
|
+
and other dependencies.
|
225
|
+
|
226
|
+
db
|
227
|
+
Contains the database schema in schema.rb. db/migrate contains all the
|
228
|
+
sequence of Migrations for your schema.
|
229
|
+
|
230
|
+
doc
|
231
|
+
This directory is where your application documentation will be stored when
|
232
|
+
generated using <tt>rake doc:app</tt>
|
233
|
+
|
234
|
+
lib
|
235
|
+
Application specific libraries. Basically, any kind of custom code that
|
236
|
+
doesn't belong under controllers, models, or helpers. This directory is in
|
237
|
+
the load path.
|
238
|
+
|
239
|
+
public
|
240
|
+
The directory available for the web server. Contains subdirectories for
|
241
|
+
images, stylesheets, and javascripts. Also contains the dispatchers and the
|
242
|
+
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
243
|
+
server.
|
244
|
+
|
245
|
+
script
|
246
|
+
Helper scripts for automation and generation.
|
247
|
+
|
248
|
+
test
|
249
|
+
Unit and functional tests along with fixtures. When using the rails generate
|
250
|
+
command, template test files will be generated for you and placed in this
|
251
|
+
directory.
|
252
|
+
|
253
|
+
vendor
|
254
|
+
External libraries that the application depends on. Also includes the plugins
|
255
|
+
subdirectory. If the app has frozen rails, those gems also go here, under
|
256
|
+
vendor/rails/. This directory is in the load path.
|