qor_cache 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +14 -0
- data/config/qor/cache.rb +31 -0
- data/config/qor/test.rb +25 -0
- data/lib/qor_cache/active_record.rb +68 -0
- data/lib/qor_cache/configuration.rb +16 -0
- data/lib/qor_cache/kernel.rb +17 -0
- data/lib/qor_cache/railtie.rb +17 -0
- data/lib/qor_cache/version.rb +5 -0
- data/lib/qor_cache.rb +73 -0
- data/qor_cache.gemspec +24 -0
- data/test/dummy/.gitignore +2 -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/mailers/.gitkeep +0 -0
- data/test/dummy/app/models/.gitkeep +0 -0
- data/test/dummy/app/models/color_variation.rb +16 -0
- data/test/dummy/app/models/product.rb +20 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config/application.rb +59 -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/qor/cache.rb +11 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20121025062539_create_products.rb +10 -0
- data/test/dummy/db/migrate/20121025062642_create_color_variations.rb +12 -0
- data/test/dummy/lib/assets/.gitkeep +0 -0
- data/test/dummy/log/.gitkeep +0 -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/dummy/test/fixtures/color_variations.yml +11 -0
- data/test/dummy/test/fixtures/products.yml +9 -0
- data/test/dummy/test/unit/color_variation_test.rb +7 -0
- data/test/dummy/test/unit/product_test.rb +7 -0
- data/test/factories.rb +13 -0
- data/test/qor_cache_fields_test.rb +29 -0
- data/test/qor_cache_key_test.rb +64 -0
- data/test/qor_cache_methods_test.rb +140 -0
- data/test/test_helper.rb +18 -0
- metadata +217 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jinzhu
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Qor Cache
|
2
|
+
=========
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
# config/qor/cache.rb
|
7
|
+
cache_key "product" do
|
8
|
+
[Product, Collection]
|
9
|
+
end
|
10
|
+
|
11
|
+
cache_key "current_season" do
|
12
|
+
Season.current
|
13
|
+
end
|
14
|
+
|
15
|
+
scope :color_variation do
|
16
|
+
cache_method :heavy_method_related_to_products, 'product'
|
17
|
+
cache_method :heavy_method_related_to_product do |s|
|
18
|
+
s.product
|
19
|
+
end
|
20
|
+
|
21
|
+
cache_class_method :heavy_class_method
|
22
|
+
cache_class_method :heavy_class_method_related_to_current_season, 'current_season'
|
23
|
+
|
24
|
+
cache_field :product_code, :from => [:product, :code]
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
# app/models/color_variation.rb
|
29
|
+
def heavy_method_related_to_products
|
30
|
+
Product.all.map(&:id).sum
|
31
|
+
end
|
32
|
+
|
33
|
+
def heavy_method_related_to_product
|
34
|
+
product.size_variations.map(&:id).sum
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.heavy_class_method
|
38
|
+
self.all.map {|x| x.id}.sum
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.heavy_class_method_related_to_current_season
|
42
|
+
where(:season_id => Season.current.id).map {|x| x.id}.sum
|
43
|
+
end
|
44
|
+
|
45
|
+
# app/views/xxx.html.erb
|
46
|
+
cache qor_cache_key('product', 'current_season') do
|
47
|
+
xxxxx
|
48
|
+
end
|
49
|
+
|
50
|
+
cache qor_cache_key('product') { current_user.role } do
|
51
|
+
xxxxx
|
52
|
+
end
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rubygems/package_task'
|
5
|
+
require 'rake/testtask'
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.libs << 'test'
|
10
|
+
t.pattern = 'test/**/*_test.rb'
|
11
|
+
t.verbose = false
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => :test
|
data/config/qor/cache.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
cache_key :product do
|
2
|
+
Product
|
3
|
+
end
|
4
|
+
|
5
|
+
cache_key :color_variation do
|
6
|
+
[ColorVariation]
|
7
|
+
end
|
8
|
+
|
9
|
+
cache_key :product_and_color_variation do
|
10
|
+
[Product, ColorVariation]
|
11
|
+
end
|
12
|
+
|
13
|
+
scope :product do
|
14
|
+
cache_method :slow_method
|
15
|
+
cache_method :slow_method_with_args
|
16
|
+
|
17
|
+
cache_class_method :slow_class_method
|
18
|
+
cache_class_method :slow_class_method_with_args
|
19
|
+
end
|
20
|
+
|
21
|
+
scope :color_variation do
|
22
|
+
cache_method :slow_method, 'product'
|
23
|
+
|
24
|
+
cache_method :slow_method_with_product do |s|
|
25
|
+
s.product
|
26
|
+
end
|
27
|
+
|
28
|
+
cache_class_method :slow_class_method, 'product'
|
29
|
+
|
30
|
+
cache_field :product_code, :from => [:product, :code]
|
31
|
+
end
|
data/config/qor/test.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
# Run your tests with nokogiri's 1.4 branch and master branch
|
5
|
+
gem 'nokogiri', :git => "git://github.com/sparklemotion/nokogiri.git", :branch => "1.4"
|
6
|
+
gem 'nokogiri', :git => "git://github.com/sparklemotion/nokogiri.git", :branch => "master"
|
7
|
+
|
8
|
+
# You can also write above code in one line like this
|
9
|
+
gem 'nokogiri', [{:git => "git://github.com/sparklemotion/nokogiri.git"}, {:git => "git://github.com/sparklemotion/nokogiri.git", :branch => "1.4"}]
|
10
|
+
|
11
|
+
# Default mode, your tests will run with rails 3.1, 3.2 and 4.0
|
12
|
+
# So with above config, your tests will run 6 times
|
13
|
+
# [nokogiri(1.4), rails(3.1)], [nokogiri(master), rails(3.1)]
|
14
|
+
# [nokogiri(1.4), rails(3.2)], [nokogiri(master), rails(3.2)]
|
15
|
+
# [nokogiri(1.4), rails(4.0)], [nokogiri(master), rails(4.0)]
|
16
|
+
env 'default' do
|
17
|
+
ruby '1.9.3'
|
18
|
+
gem 'rails', ['3.1', '3.2', '4.0']
|
19
|
+
end
|
20
|
+
|
21
|
+
# Using `qor_test -e old_ruby -c 'rake test'` to run your tests in old_ruby mode
|
22
|
+
env 'old_ruby' do
|
23
|
+
ruby 'ree'
|
24
|
+
gem 'rails', ['3.1', '3.2']
|
25
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Qor
|
2
|
+
module Cache
|
3
|
+
module ActiveRecord
|
4
|
+
def self.included(base)
|
5
|
+
base.extend SingletonMethods
|
6
|
+
base.send :include, InstanceMethods
|
7
|
+
base.after_save :_qor_cache_expire
|
8
|
+
base.after_destroy :_qor_cache_expire
|
9
|
+
|
10
|
+
base.before_create :_qor_cache_sync_cached_fields
|
11
|
+
base.after_save :_qor_cache_sync_qor_cache_fields
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module SingletonMethods
|
16
|
+
def _qor_cache_key
|
17
|
+
"#{self.name}-cache_key"
|
18
|
+
end
|
19
|
+
|
20
|
+
def cache_key
|
21
|
+
Qor::Cache::Base.cache_store.fetch(_qor_cache_key) do
|
22
|
+
key = self.name.to_s
|
23
|
+
key << Time.now.to_i.to_s
|
24
|
+
key << rand.to_s
|
25
|
+
Digest::SHA1.hexdigest(key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def _qor_cache_expire
|
30
|
+
Qor::Cache::Base.cache_store.delete(_qor_cache_key)
|
31
|
+
true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
module InstanceMethods
|
36
|
+
def _qor_cache_expire
|
37
|
+
self.class._qor_cache_expire
|
38
|
+
end
|
39
|
+
|
40
|
+
def _qor_cache_sync_cached_fields
|
41
|
+
nodes = Qor::Cache::Configuration.deep_find(:cache_field) do |node|
|
42
|
+
node.parent.is_node?(:scope, self.class.name.demodulize.underscore)
|
43
|
+
end
|
44
|
+
|
45
|
+
nodes.map do |node|
|
46
|
+
obj = node.options[:from].inject(self) { |obj, value| obj.try(value) }
|
47
|
+
self.send("#{node.name}=", obj)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def _qor_cache_sync_qor_cache_fields
|
52
|
+
nodes = Qor::Cache::Configuration.deep_find(:cache_field) do |node|
|
53
|
+
node.options[:from][-2].to_sym == self.class.name.demodulize.underscore.to_sym
|
54
|
+
end
|
55
|
+
|
56
|
+
nodes.map do |node|
|
57
|
+
node_model = node.parent.name.to_s.classify.constantize
|
58
|
+
fk = self.class.reflections.select {|name, ref| ref.klass == node_model}.first.last.foreign_key
|
59
|
+
|
60
|
+
node_method = node.options[:from][-1]
|
61
|
+
updates = {node.name => try(node_method)}
|
62
|
+
|
63
|
+
node_model.update_all(updates, ["#{fk} = ?", self.id]) if updates.present?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "digest/md5"
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
def qor_cache_key(*names, &blk)
|
5
|
+
objs = names.map { |name| Qor::Cache::Configuration.first(:cache_key, name).block.call }
|
6
|
+
objs << blk.call if block_given?
|
7
|
+
|
8
|
+
results = objs.map do |obj|
|
9
|
+
Array(obj).map {|x| x.respond_to?(:cache_key) ? x.cache_key : x.inspect }.join("-")
|
10
|
+
end
|
11
|
+
|
12
|
+
Digest::MD5.hexdigest(results.join("-"))
|
13
|
+
rescue
|
14
|
+
puts $!
|
15
|
+
rand()
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Qor
|
2
|
+
module Cache
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
|
5
|
+
initializer "insert_active_record_methods_for_qor_cache" do
|
6
|
+
ActiveSupport.on_load(:active_record) do
|
7
|
+
::ActiveRecord::Base.send :include, ::Qor::Cache::ActiveRecord
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
initializer "qor_cache_init_cache_method", :after => "insert_active_record_methods_for_qor_cache" do
|
12
|
+
Qor::Cache::Base.init_cache_methods
|
13
|
+
Qor::Cache::Base.init_cache_class_methods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/qor_cache.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require "qor_cache/configuration"
|
2
|
+
require "qor_cache/active_record"
|
3
|
+
require "qor_cache/railtie"
|
4
|
+
require "qor_cache/kernel"
|
5
|
+
require "digest/md5"
|
6
|
+
|
7
|
+
module Qor
|
8
|
+
module Cache
|
9
|
+
class Base
|
10
|
+
def self.cache_store
|
11
|
+
@cache_store || Rails.cache
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.init_cache_methods
|
15
|
+
Qor::Cache::Configuration.find(:scope).map do |m|
|
16
|
+
model = m.name.to_s.classify.constantize
|
17
|
+
|
18
|
+
m.find(:cache_method).map do |node|
|
19
|
+
method = node.name
|
20
|
+
original_method = "_uncached_#{method}_for_qor_cache".to_sym
|
21
|
+
|
22
|
+
model.class_eval do
|
23
|
+
alias_method original_method, method
|
24
|
+
|
25
|
+
define_method(method) do |*args|
|
26
|
+
_cache_key = Digest::MD5.hexdigest([
|
27
|
+
method,
|
28
|
+
node.block ? qor_cache_key { node.block.call(self) } : nil,
|
29
|
+
node.data.map {|x| qor_cache_key(x) },
|
30
|
+
cache_key,
|
31
|
+
args.map(&:inspect).join("-")
|
32
|
+
].map(&:to_s).join("-"))
|
33
|
+
|
34
|
+
Qor::Cache::Base.cache_store.fetch(_cache_key) do
|
35
|
+
self.send(original_method, *args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.init_cache_class_methods
|
44
|
+
Qor::Cache::Configuration.find(:scope).map do |m|
|
45
|
+
model = m.name.to_s.classify.constantize
|
46
|
+
m.find(:cache_class_method).map do |node|
|
47
|
+
method = node.name
|
48
|
+
original_method = "_uncached_#{method}_for_qor_cache".to_sym
|
49
|
+
|
50
|
+
model.instance_eval <<-EOS, __FILE__, __LINE__ + 1
|
51
|
+
class << self
|
52
|
+
alias :#{original_method} :#{method}
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.#{method}(*args)
|
56
|
+
_cache_key = Digest::MD5.hexdigest([
|
57
|
+
:#{method},
|
58
|
+
#{node.data}.map {|x| qor_cache_key(x) },
|
59
|
+
cache_key,
|
60
|
+
args.map(&:inspect).join("-")
|
61
|
+
].map(&:to_s).join("-"))
|
62
|
+
|
63
|
+
Qor::Cache::Base.cache_store.fetch(_cache_key) do
|
64
|
+
self.send(:#{original_method}, *args)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
EOS
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/qor_cache.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'qor_cache/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "qor_cache"
|
8
|
+
gem.version = Qor::Cache::VERSION
|
9
|
+
gem.authors = ["Jinzhu"]
|
10
|
+
gem.email = ["wosmvp@gmail.com"]
|
11
|
+
gem.description = %q{Qor Cache}
|
12
|
+
gem.summary = %q{Qor Cache}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "qor_test"
|
21
|
+
gem.add_dependency "qor_dsl"
|
22
|
+
gem.add_dependency "rails"
|
23
|
+
gem.add_development_dependency "sqlite3"
|
24
|
+
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.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
+
|
5
|
+
require File.expand_path('../config/application', __FILE__)
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|