bento_search 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +299 -0
- data/Rakefile +40 -0
- data/app/assets/images/bento_search/large_loader.gif +0 -0
- data/app/assets/javascripts/bento_search.js +3 -0
- data/app/assets/javascripts/bento_search/ajax_load.js +22 -0
- data/app/assets/stylesheets/bento_search/bento.css +4 -0
- data/app/controllers/bento_search/bento_search_controller.rb +7 -0
- data/app/controllers/bento_search/search_controller.rb +72 -0
- data/app/helpers/bento_search_helper.rb +138 -0
- data/app/item_decorators/bento_search/only_premade_openurl.rb +16 -0
- data/app/item_decorators/bento_search/openurl_add_other_link.rb +35 -0
- data/app/item_decorators/bento_search/openurl_main_link.rb +30 -0
- data/app/models/bento_search/author.rb +25 -0
- data/app/models/bento_search/link.rb +30 -0
- data/app/models/bento_search/multi_searcher.rb +109 -0
- data/app/models/bento_search/openurl_creator.rb +128 -0
- data/app/models/bento_search/registrar.rb +70 -0
- data/app/models/bento_search/result_item.rb +203 -0
- data/app/models/bento_search/results.rb +54 -0
- data/app/models/bento_search/results/pagination.rb +67 -0
- data/app/models/bento_search/search_engine.rb +219 -0
- data/app/models/bento_search/search_engine/capabilities.rb +65 -0
- data/app/search_engines/bento_search/#Untitled-1# +11 -0
- data/app/search_engines/bento_search/ebsco_host_engine.rb +356 -0
- data/app/search_engines/bento_search/eds_engine.rb +557 -0
- data/app/search_engines/bento_search/google_books_engine.rb +184 -0
- data/app/search_engines/bento_search/primo_engine.rb +231 -0
- data/app/search_engines/bento_search/scopus_engine.rb +295 -0
- data/app/search_engines/bento_search/summon_engine.rb +398 -0
- data/app/search_engines/bento_search/xerxes_engine.rb +168 -0
- data/app/views/bento_search/_link.html.erb +4 -0
- data/app/views/bento_search/_search_error.html.erb +22 -0
- data/app/views/bento_search/_std_item.html.erb +39 -0
- data/app/views/bento_search/search/search.html.erb +1 -0
- data/config/locales/en.yml +25 -0
- data/lib/bento_search.rb +29 -0
- data/lib/bento_search/engine.rb +5 -0
- data/lib/bento_search/routes.rb +45 -0
- data/lib/bento_search/version.rb +3 -0
- data/lib/generators/bento_search/pull_ebsco_dbs_generator.rb +24 -0
- data/lib/generators/bento_search/templates/ebsco_global_var.erb +6 -0
- data/lib/http_client_patch/include_client.rb +86 -0
- data/lib/tasks/bento_search_tasks.rake +4 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +56 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +6 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +3100 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/functional/bento_search/search_controller_test.rb +81 -0
- data/test/helper/bento_search_helper_test.rb +125 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/support/mock_engine.rb +23 -0
- data/test/support/test_with_cassette.rb +38 -0
- data/test/test_helper.rb +52 -0
- data/test/unit/#vcr_test.rb# +68 -0
- data/test/unit/ebsco_host_engine_test.rb +134 -0
- data/test/unit/eds_engine_test.rb +105 -0
- data/test/unit/google_books_engine_test.rb +93 -0
- data/test/unit/item_decorators_test.rb +66 -0
- data/test/unit/multi_searcher_test.rb +49 -0
- data/test/unit/openurl_creator_test.rb +111 -0
- data/test/unit/pagination_test.rb +59 -0
- data/test/unit/primo_engine_test.rb +37 -0
- data/test/unit/register_engine_test.rb +50 -0
- data/test/unit/result_item_display_test.rb +39 -0
- data/test/unit/result_item_test.rb +36 -0
- data/test/unit/scopus_engine_test.rb +130 -0
- data/test/unit/search_engine_base_test.rb +178 -0
- data/test/unit/search_engine_test.rb +95 -0
- data/test/unit/summon_engine_test.rb +161 -0
- data/test/unit/xerxes_engine_test.rb +70 -0
- data/test/vcr_cassettes/ebscohost/error_bad_db.yml +45 -0
- data/test/vcr_cassettes/ebscohost/error_bad_password.yml +45 -0
- data/test/vcr_cassettes/ebscohost/get_info.yml +3626 -0
- data/test/vcr_cassettes/ebscohost/live_search.yml +45 -0
- data/test/vcr_cassettes/ebscohost/live_search_smoke_test.yml +1311 -0
- data/test/vcr_cassettes/eds/basic_search_smoke_test.yml +1811 -0
- data/test/vcr_cassettes/eds/get_auth_token.yml +75 -0
- data/test/vcr_cassettes/eds/get_auth_token_failure.yml +39 -0
- data/test/vcr_cassettes/eds/get_with_auth.yml +243 -0
- data/test/vcr_cassettes/eds/get_with_auth_recovers_from_bad_auth.yml +368 -0
- data/test/vcr_cassettes/gbs/error_condition.yml +40 -0
- data/test/vcr_cassettes/gbs/pagination.yml +702 -0
- data/test/vcr_cassettes/gbs/search.yml +340 -0
- data/test/vcr_cassettes/primo/search_smoke_test.yml +1112 -0
- data/test/vcr_cassettes/scopus/bad_api_key_should_return_error_response.yml +60 -0
- data/test/vcr_cassettes/scopus/escaped_chars.yml +187 -0
- data/test/vcr_cassettes/scopus/fielded_search.yml +176 -0
- data/test/vcr_cassettes/scopus/simple_search.yml +227 -0
- data/test/vcr_cassettes/scopus/zero_results_search.yml +67 -0
- data/test/vcr_cassettes/summon/bad_auth.yml +54 -0
- data/test/vcr_cassettes/summon/proper_tags_for_snippets.yml +216 -0
- data/test/vcr_cassettes/summon/search.yml +242 -0
- data/test/vcr_cassettes/xerxes/live_search.yml +2580 -0
- data/test/view/std_item_test.rb +98 -0
- metadata +421 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
<div class="bento_search_error alert alert-error">
|
2
|
+
<p>
|
3
|
+
<%= t("bento_search.search_error") %>
|
4
|
+
</p>
|
5
|
+
|
6
|
+
<div style="overflow-x:scroll">
|
7
|
+
<p>
|
8
|
+
<%= results.error %>
|
9
|
+
</p>
|
10
|
+
<p>
|
11
|
+
|
12
|
+
<% if Rails.application.config.consider_all_requests_local && results.error["exception"] %>
|
13
|
+
<ul>
|
14
|
+
<% results.error["exception"].backtrace.each do |line| %>
|
15
|
+
<li><%= line %></li>
|
16
|
+
<% end %>
|
17
|
+
<ul>
|
18
|
+
<% end %>
|
19
|
+
</p>
|
20
|
+
</div>
|
21
|
+
|
22
|
+
</div>
|
@@ -0,0 +1,39 @@
|
|
1
|
+
<% # must pass in local 'item' OR 'std_item' (as used by rails collection
|
2
|
+
# rendering) that's a BentoSearch::ResultItem
|
3
|
+
|
4
|
+
item = local_assigns[:item] || local_assigns[:std_item]
|
5
|
+
|
6
|
+
%>
|
7
|
+
|
8
|
+
<div class="bento_item">
|
9
|
+
<%= bento_item_title(item) %>
|
10
|
+
|
11
|
+
<div class="bento_item_body">
|
12
|
+
|
13
|
+
|
14
|
+
<% if item.authors.length > 0 %>
|
15
|
+
<p class="bento_item_authors">
|
16
|
+
<%= bento_item_authors(item) %>
|
17
|
+
</p>
|
18
|
+
<% end %>
|
19
|
+
|
20
|
+
<% if item.published_in %>
|
21
|
+
<p class="bento_item_row published_in">
|
22
|
+
<span class="bento_value"><%= item.published_in %> </span>
|
23
|
+
</p>
|
24
|
+
<% end %>
|
25
|
+
|
26
|
+
<% if item.abstract %>
|
27
|
+
<p class="bento_item_abstract">
|
28
|
+
<%= bento_abstract_truncate( item.abstract ) %>
|
29
|
+
</p>
|
30
|
+
<% end %>
|
31
|
+
|
32
|
+
<% if item.other_links.present? %>
|
33
|
+
<p class="bento_item_other_links">
|
34
|
+
<%= render :partial => "bento_search/link", :collection => item.other_links %>
|
35
|
+
</p>
|
36
|
+
<% end %>
|
37
|
+
</div>
|
38
|
+
|
39
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= bento_search(@results) %>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
en:
|
2
|
+
bento_search:
|
3
|
+
ajax_loading: "Loading..."
|
4
|
+
no_results: "No results found."
|
5
|
+
search_error: "Sorry! An error occured and results could not be retrieved."
|
6
|
+
missing_title: "[Title Missing]"
|
7
|
+
format:
|
8
|
+
# Schema.org formats
|
9
|
+
Article: Article
|
10
|
+
Book: Book
|
11
|
+
Movie: Movie
|
12
|
+
MusicRecording: Musical Recording
|
13
|
+
Photograph: Photo
|
14
|
+
SoftwareApplication: Software
|
15
|
+
# And some custom ones that Schema.org didn't cover, alas
|
16
|
+
serial: Magazine or Journal
|
17
|
+
dissertation: Dissertation or Thesis
|
18
|
+
sort_keys:
|
19
|
+
title_asc: "title"
|
20
|
+
date_desc: "publication date"
|
21
|
+
relevance: "relevance"
|
22
|
+
author_asc: "author"
|
23
|
+
num_cite_desc: "citation count"
|
24
|
+
eds:
|
25
|
+
record_not_available: "Record Not Available -- log in to see full results"
|
data/lib/bento_search.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "bento_search/engine"
|
2
|
+
require 'bento_search/routes'
|
3
|
+
require 'confstruct'
|
4
|
+
|
5
|
+
# ugh, sorry:
|
6
|
+
require File.dirname(__FILE__) + '/../app/models/bento_search/registrar'
|
7
|
+
|
8
|
+
module BentoSearch
|
9
|
+
|
10
|
+
def self.global_registrar
|
11
|
+
@@global_registrar ||= BentoSearch::Registrar.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.register_engine(id, &block)
|
15
|
+
global_registrar.register_engine(id, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.get_engine(id)
|
19
|
+
global_registrar.get_engine(id)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Mostly just used for testing
|
23
|
+
def self.reset_engine_registrations!
|
24
|
+
global_registrar.reset_engine_registrations!
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module BentoSearch
|
3
|
+
class Routes
|
4
|
+
|
5
|
+
def initialize(router, options = {})
|
6
|
+
@router = router
|
7
|
+
@options = {:scope => "/bento"}.merge(options)
|
8
|
+
end
|
9
|
+
|
10
|
+
def draw
|
11
|
+
route_sets.each do |r|
|
12
|
+
self.send(r)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
|
18
|
+
def add_routes &blk
|
19
|
+
@router.instance_exec(@options, &blk)
|
20
|
+
end
|
21
|
+
|
22
|
+
def route_sets
|
23
|
+
(@options[:only] || default_route_sets) - (@options[:except] || [])
|
24
|
+
end
|
25
|
+
|
26
|
+
def default_route_sets
|
27
|
+
# :search should always be LAST
|
28
|
+
[:search]
|
29
|
+
end
|
30
|
+
|
31
|
+
module RouteSets
|
32
|
+
|
33
|
+
def search
|
34
|
+
add_routes do |options|
|
35
|
+
scope options[:scope] do
|
36
|
+
get ":engine_id" => "bento_search/search#search", :as => "to_bento_search"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
include RouteSets
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
module BentoSearch
|
3
|
+
class PullEbscoDbsGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
desc "Pull avail dbs from EBSCO info task, write to source file that defines an array in ruby global variable."
|
7
|
+
|
8
|
+
argument :engine_id, :type => :string
|
9
|
+
|
10
|
+
class_option :output_file, :type => :string, :default => "./config/ebsco_dbs.rb", :description => "filepath to write ruby file"
|
11
|
+
class_option :global_name, :type => :string, :default => "$ebsco_dbs", :description => "global variable to set in generated source file"
|
12
|
+
|
13
|
+
def generate
|
14
|
+
engine = BentoSearch.get_engine( engine_id )
|
15
|
+
|
16
|
+
@dbs = engine.get_info.xpath("./info/dbInfo/db") #.sort_by {|n| n["shortName"]}
|
17
|
+
|
18
|
+
|
19
|
+
template("ebsco_global_var.erb", options.output_file)
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# Submitted to HTTPClient, if it doesn't make it there i may make it it's own
|
2
|
+
# gem, in the meantime it's here as HTTPClientPatch::IncludeClient
|
3
|
+
|
4
|
+
# It is useful to re-use a HTTPClient instance for multiple requests, to
|
5
|
+
# re-use HTTP 1.1 persistent connections.
|
6
|
+
#
|
7
|
+
# To do that, you sometimes want to store an HTTPClient instance in a global/
|
8
|
+
# class variable location, so it can be accessed and re-used.
|
9
|
+
#
|
10
|
+
# This mix-in makes it easy to create class-level access to one or more
|
11
|
+
# HTTPClient instances. The HTTPClient instances are lazily initialized
|
12
|
+
# on first use (to, for instance, avoid interfering with WebMock/VCR),
|
13
|
+
# and are initialized in a thread-safe manner. Note that a
|
14
|
+
# HTTPClient, once initialized, is safe for use in multiple threads.
|
15
|
+
#
|
16
|
+
# Note that you `extend` HTTPClient::IncludeClient, not `include.
|
17
|
+
#
|
18
|
+
# require 'httpclient/include_client'
|
19
|
+
# class Widget
|
20
|
+
# extend HTTPClient::IncludeClient
|
21
|
+
#
|
22
|
+
# include_http_client
|
23
|
+
# # and/or, specify more stuff
|
24
|
+
# include_http_client('http://myproxy:8080', :method_name => :my_client) do |client|
|
25
|
+
# # any init you want
|
26
|
+
# client.set_cookie_store nil
|
27
|
+
# client.
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# That creates two HTTPClient instances available at the class level.
|
32
|
+
# The first will be available from Widget.http_client (default method
|
33
|
+
# name for `include_http_client`), with default initialization.
|
34
|
+
#
|
35
|
+
# The second will be available at Widget.my_client, with the init arguments
|
36
|
+
# provided, further initialized by the block provided.
|
37
|
+
#
|
38
|
+
# In addition to a class-level method, for convenience instance-level methods
|
39
|
+
# are also provided. Widget.http_client is identical to Widget.new.http_client
|
40
|
+
#
|
41
|
+
#
|
42
|
+
class HTTPClientPatch
|
43
|
+
module IncludeClient
|
44
|
+
|
45
|
+
|
46
|
+
def include_http_client(*args, &block)
|
47
|
+
# We're going to dynamically define a class
|
48
|
+
# to hold our state, namespaced, as well as possibly dynamic
|
49
|
+
# name of cover method.
|
50
|
+
method_name = (args.last.delete(:method_name) if args.last.kind_of? Hash) || :http_client
|
51
|
+
args.pop if args.last == {} # if last arg was named methods now empty, remove it.
|
52
|
+
|
53
|
+
# By the amazingness of closures, we can create these things
|
54
|
+
# in local vars here and use em in our method, we don't even
|
55
|
+
# need iVars for state.
|
56
|
+
client_instance = nil
|
57
|
+
client_mutex = Mutex.new
|
58
|
+
client_args = args
|
59
|
+
client_block = block
|
60
|
+
|
61
|
+
# to define a _class method_ on the specific class that's currently
|
62
|
+
# `self`, we have to use this bit of metaprogramming, sorry.
|
63
|
+
(class << self; self ; end).instance_eval do
|
64
|
+
define_method(method_name) do
|
65
|
+
# implementation copied from ruby stdlib singleton
|
66
|
+
# to create this global obj thread-safely.
|
67
|
+
return client_instance if client_instance
|
68
|
+
client_mutex.synchronize do
|
69
|
+
return client_instance if client_instance
|
70
|
+
# init HTTPClient with specified args/block
|
71
|
+
client_instance = HTTPClient.new(*client_args)
|
72
|
+
client_block.call(client_instance) if client_block
|
73
|
+
end
|
74
|
+
return client_instance
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# And for convenience, an _instance method_ on the class that just
|
79
|
+
# delegates to the class method.
|
80
|
+
define_method(method_name) do
|
81
|
+
self.class.send(method_name)
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
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
|
+
| `-- 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.
|