redis_dictionary 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.md +37 -0
- data/Rakefile +41 -0
- data/app/assets/javascripts/redis_dictionary/application.js +16 -0
- data/app/assets/javascripts/redis_dictionary/bootstrap.js +10 -0
- data/app/assets/javascripts/redis_dictionary/jquery.autosize.js +192 -0
- data/app/assets/javascripts/redis_dictionary/translations.js +35 -0
- data/app/assets/stylesheets/redis_dictionary/application.css +13 -0
- data/app/assets/stylesheets/redis_dictionary/bootstrap_and_overrides.css +7 -0
- data/app/assets/stylesheets/redis_dictionary/translations.css +42 -0
- data/app/controllers/redis_dictionary/application_controller.rb +25 -0
- data/app/controllers/redis_dictionary/translations_controller.rb +51 -0
- data/app/helpers/redis_dictionary/application_helper.rb +39 -0
- data/app/models/redis_dictionary/translation.rb +64 -0
- data/app/views/layouts/redis_dictionary/application.html.erb +43 -0
- data/app/views/redis_dictionary/translations/_key_translation.html.erb +22 -0
- data/app/views/redis_dictionary/translations/create.js.erb +14 -0
- data/app/views/redis_dictionary/translations/destroy.js.erb +2 -0
- data/app/views/redis_dictionary/translations/index.html.erb +69 -0
- data/config/environment.rb +1 -0
- data/config/initializers/will_paginate.rb +1 -0
- data/config/routes.rb +14 -0
- data/lib/redis_dictionary.rb +25 -0
- data/lib/redis_dictionary/configuration.rb +21 -0
- data/lib/redis_dictionary/engine.rb +53 -0
- data/lib/redis_dictionary/version.rb +3 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/images/favicon.ico +0 -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 +4 -0
- data/test/dummy/app/helpers/application_helper.rb +3 -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 +59 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +6 -0
- data/test/dummy/config/environments/development.rb +33 -0
- data/test/dummy/config/environments/production.rb +70 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +8 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +6 -0
- data/test/dummy/config/initializers/secret_token.rb +9 -0
- data/test/dummy/config/initializers/session_store.rb +9 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +15 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +4 -0
- data/test/dummy/db/schema.rb +16 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +1003 -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 +7 -0
- data/test/functional/redis_dictionary/translations_controller_test.rb +37 -0
- data/test/integration/redis_dictionary/translations_test.rb +113 -0
- data/test/redis_dictionary/configuration_test.rb +24 -0
- data/test/redis_dictionary_test.rb +22 -0
- data/test/test_helper.rb +43 -0
- data/test/unit/helpers/redis_dictionary/application_helper_test.rb +61 -0
- data/test/unit/redis_dictionary/translation_test.rb +89 -0
- metadata +360 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_dependency "redis_dictionary/application_controller"
|
3
|
+
|
4
|
+
module RedisDictionary
|
5
|
+
class TranslationsController < RedisDictionary::ApplicationController
|
6
|
+
respond_to :json, :html, :js
|
7
|
+
|
8
|
+
redis_dictionary_authenticate
|
9
|
+
|
10
|
+
def index
|
11
|
+
unless params[:query].blank?
|
12
|
+
@translations = (params[:query] == '*') ?
|
13
|
+
Translation.all.select{|j| j =~ /^#{params[:query]}$/}.paginate(:page => params[:page], :per_page => 15) :
|
14
|
+
Translation.all.select{|j| j =~ /#{params[:query]}/}.paginate(:page => params[:page], :per_page => 15)
|
15
|
+
else
|
16
|
+
@translations = Translation.all.paginate(:page => params[:page], :per_page => 15)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def create
|
21
|
+
|
22
|
+
Translation.create(params[:key], params[:value], params[:locale])
|
23
|
+
respond_to do |format|
|
24
|
+
format.js{
|
25
|
+
@digest = RedisDictionary::Translation.digest_key(params[:key])
|
26
|
+
}
|
27
|
+
format.html{
|
28
|
+
redirect_to redis_dictionary_root_path, :notice => "Added translations"
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def destroy
|
35
|
+
Translation.destroy(params[:id])
|
36
|
+
respond_to do |format|
|
37
|
+
format.js
|
38
|
+
format.html{
|
39
|
+
redirect_to redis_dictionary_root_path, :notice => "Key deleted"
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def get
|
46
|
+
locale ||= params[:lang]
|
47
|
+
respond_with t params[:key], locale: locale
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module RedisDictionary
|
4
|
+
module ApplicationHelper
|
5
|
+
def title(page_title, show_title = true)
|
6
|
+
@show_title = show_title
|
7
|
+
if @show_title
|
8
|
+
content_for(:title) { h(page_title.to_s) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def show_title?
|
13
|
+
@show_title
|
14
|
+
end
|
15
|
+
|
16
|
+
def humanize_key(key)
|
17
|
+
key.split('.').last.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
|
18
|
+
end
|
19
|
+
|
20
|
+
# Can search for named routes directly in the main app, omitting
|
21
|
+
# the "main_app." prefix
|
22
|
+
def method_missing method, *args, &block
|
23
|
+
if main_app_url_helper?(method)
|
24
|
+
main_app.send(method, *args)
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def main_app_url_helper?(method)
|
33
|
+
RedisDictionary::configuration.inline_main_app_named_routes and
|
34
|
+
(method.to_s.end_with?('_path') or method.to_s.end_with?('_url')) and
|
35
|
+
main_app.respond_to?(method)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'digest/md5'
|
4
|
+
|
5
|
+
module RedisDictionary
|
6
|
+
class Translation
|
7
|
+
|
8
|
+
|
9
|
+
def self.all(string = nil)
|
10
|
+
keys = Engine.redis.sunion('keys', 'system_keys')
|
11
|
+
if string.present?
|
12
|
+
keys = keys.grep(/#{string}/)
|
13
|
+
end
|
14
|
+
keys.sort
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.keys
|
18
|
+
Engine.redis.smembers('keys').sort!
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.system_keys
|
22
|
+
Engine.redis.smembers('system_keys').sort!
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.system_key?(key)
|
26
|
+
Engine.redis.sismember('system_keys', key)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.create(key, value, locale)
|
30
|
+
if key.present?
|
31
|
+
digest = digest_key(key)
|
32
|
+
Engine.redis.hset('keys_digest', digest, key)
|
33
|
+
Engine.redis.sadd('keys', key)
|
34
|
+
I18n.backend.store_translations(locale, {key => value}, :escape => false)
|
35
|
+
digest
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.exists?(key, locale)
|
40
|
+
Engine.redis.exists("#{locale}.#{key}")
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.destroy(digest)
|
44
|
+
key = Engine.redis.hget('keys_digest', digest)
|
45
|
+
Engine.redis.srem('keys', key)
|
46
|
+
Engine.redis.hdel("keys_digest", digest)
|
47
|
+
I18n.available_locales.each do |locale|
|
48
|
+
Engine.redis.del("#{locale}.#{key}")
|
49
|
+
end
|
50
|
+
I18n.reload!
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.digest_key(key)
|
54
|
+
Digest::MD5.hexdigest(key)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.rehash!
|
58
|
+
self.all.each do |key|
|
59
|
+
Engine.redis.hset('keys_digest', digest_key(key), key)
|
60
|
+
end
|
61
|
+
true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2
|
+
<html lang="<%= I18n.locale %>">
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8"/>
|
5
|
+
<title>
|
6
|
+
<%= content_for?(:title) ? (yield(:title) + " - I18n Dashboard") : "I18n Dashboard" %>
|
7
|
+
</title>
|
8
|
+
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
|
9
|
+
<meta content="Fourmach.com" name="author"/>
|
10
|
+
<%= stylesheet_link_tag "redis_dictionary/application", media: nil %>
|
11
|
+
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements
|
12
|
+
-->
|
13
|
+
<!--[if lt IE 9]
|
14
|
+
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
15
|
+
-->
|
16
|
+
<%= csrf_meta_tag %>
|
17
|
+
</head>
|
18
|
+
<body id="<%= controller.controller_name %>-<%= controller.action_name %>">
|
19
|
+
<div class="container">
|
20
|
+
<h1>
|
21
|
+
<a href="<%= translations_path %>"><%= yield(:title) %></a>
|
22
|
+
</h1>
|
23
|
+
|
24
|
+
<div class="navbar">
|
25
|
+
<div class="navbar-inner">
|
26
|
+
<%= link_to 'All', translations_path(query: "*"), class: 'btn' %>
|
27
|
+
<%= link_to 'Activerecord', translations_path(query: "activerecord"), class: 'btn' %>
|
28
|
+
<%= link_to 'Date / Time', translations_path(query: "date|time"), class: 'btn' %>
|
29
|
+
<%= link_to 'Error', translations_path(query: "error"), class: 'btn' %>
|
30
|
+
<%= link_to 'Number', translations_path(query: "number."), class: 'btn' %>
|
31
|
+
<%= link_to 'Pagination', translations_path(query: "paginate"), class: 'btn' %>
|
32
|
+
</div>
|
33
|
+
</div>
|
34
|
+
|
35
|
+
<%= yield %>
|
36
|
+
</div>
|
37
|
+
<div id="footer">
|
38
|
+
<div class="container">
|
39
|
+
</div>
|
40
|
+
</div>
|
41
|
+
<%= javascript_include_tag "redis_dictionary/application" %>
|
42
|
+
</body>
|
43
|
+
</html>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<tr id="translation_key_<%= RedisDictionary::Translation.digest_key(key) %>">
|
2
|
+
<td>
|
3
|
+
<%= key %>
|
4
|
+
</td>
|
5
|
+
<% I18n.available_locales.each do |locale| %>
|
6
|
+
<td>
|
7
|
+
<%= link_to '#', id: "#{locale}.#{key}", data: {key: key, locale: locale, toggle: "modal", target: '#add-translation'}, class: "edit-translation #{(RedisDictionary::Translation.exists?(key, locale) ? nil : 'text-error')}" do %>
|
8
|
+
<%= t(key, locale: locale, default: humanize_key(key) ) %>
|
9
|
+
<% end %>
|
10
|
+
</td>
|
11
|
+
<% end %>
|
12
|
+
<td>
|
13
|
+
<% unless RedisDictionary::Translation.system_key?(key) %>
|
14
|
+
<%= link_to translation_path(RedisDictionary::Translation.digest_key(key)), id: "destroy-#{RedisDictionary::Translation.digest_key(key)}", method: :delete, class: 'btn btn-small btn-danger flr', data: {confirm: "Do you really want to proceed ?"}, remote: true do %>
|
15
|
+
<span class="hidden">destroy</span>
|
16
|
+
<i class="icon-trash"></i>
|
17
|
+
<% end %>
|
18
|
+
<% else %>
|
19
|
+
<div class="btn btn-inverse btn-mini flr">Framework</div>
|
20
|
+
<% end %>
|
21
|
+
</td>
|
22
|
+
</tr>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$('#add-translation').modal('hide');
|
2
|
+
|
3
|
+
partial = '<%= escape_javascript(render 'key_translation', key: params[:key]) %>';
|
4
|
+
translation_key = "#translation_key_<%= @digest %>";
|
5
|
+
|
6
|
+
if( $(translation_key).size() > 0 ){
|
7
|
+
|
8
|
+
$(translation_key).replaceWith(partial);
|
9
|
+
|
10
|
+
} else {
|
11
|
+
|
12
|
+
$('#translations-table tbody').prepend(' <%= escape_javascript(render 'key_translation', key: params[:key]) %> ');
|
13
|
+
|
14
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
<% title "Translations" %>
|
2
|
+
|
3
|
+
<div id="add-translation" class="modal hide fade">
|
4
|
+
<div class="modal-header">
|
5
|
+
<a class="close" data-dismiss="modal">×</a>
|
6
|
+
<h3>Translation</h3>
|
7
|
+
</div>
|
8
|
+
<div class="modal-body">
|
9
|
+
<div class="row">
|
10
|
+
<%= form_tag translations_path, id: 'form-translation', remote: true do %>
|
11
|
+
<div class="span1">
|
12
|
+
<%= label_tag :locale %>
|
13
|
+
<%= text_field_tag :locale, '', class: 'span1', autofocus: true %>
|
14
|
+
</div>
|
15
|
+
<div class="span4">
|
16
|
+
<%= label_tag :key %>
|
17
|
+
<%= text_field_tag :key, '', class: 'span4' %>
|
18
|
+
</div>
|
19
|
+
<div class="span5">
|
20
|
+
<%= label_tag :value %>
|
21
|
+
<%= text_area_tag :value, '', class: 'span5' %>
|
22
|
+
</div>
|
23
|
+
<div class="span2">
|
24
|
+
<br />
|
25
|
+
<%= button_tag "Submit", class: 'btn' %>
|
26
|
+
</div>
|
27
|
+
<% end %>
|
28
|
+
</div>
|
29
|
+
</div>
|
30
|
+
</div>
|
31
|
+
|
32
|
+
<div class="navbar">
|
33
|
+
<div class="navbar-inner">
|
34
|
+
<%= form_tag translations_path, class: 'navbar-form pull-left', method: :get, id: 'search-form' do %>
|
35
|
+
<%= text_field_tag :query, params[:query], hint: 'Search', placeholder: 'Search', autofocus: true %>
|
36
|
+
<%= button_tag 'search', class: 'btn' %>
|
37
|
+
<%= link_to 'clear', translations_path, class: 'btn' %>
|
38
|
+
<% end %>
|
39
|
+
<div class="pull-right">
|
40
|
+
<%= link_to 'new', '#', data: { toggle: "modal", target: '#add-translation'}, id: 'add-translation-link', class: 'btn' %>
|
41
|
+
</div>
|
42
|
+
</div>
|
43
|
+
</div>
|
44
|
+
<hr/>
|
45
|
+
|
46
|
+
<div class="apple_pagination">
|
47
|
+
<%= will_paginate @translations, :container => false %>
|
48
|
+
</div>
|
49
|
+
|
50
|
+
<table class="table table-hover" id="translations-table">
|
51
|
+
<thead>
|
52
|
+
<th>key</th>
|
53
|
+
<% I18n.available_locales.each do |locale| %>
|
54
|
+
<th>
|
55
|
+
<%= locale %>
|
56
|
+
</th>
|
57
|
+
<% end %>
|
58
|
+
</thead>
|
59
|
+
<tbody>
|
60
|
+
<% @translations.each do |key| %>
|
61
|
+
<%= render 'key_translation', key: key %>
|
62
|
+
<% end %>
|
63
|
+
</tbody>
|
64
|
+
</table>
|
65
|
+
|
66
|
+
|
67
|
+
<div class="apple_pagination">
|
68
|
+
<%= will_paginate @translations, :container => false %>
|
69
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
RedisDictionary::Engine.load!
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'will_paginate/array'
|
data/config/routes.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
RedisDictionary::Engine.routes.draw do
|
4
|
+
|
5
|
+
get "/" => "translations#index", as: :redis_dictionary_root
|
6
|
+
get "/get/(:lang)/:key" => "translations#get", lang: /[a-z]{2}/, key: /.*/ ,defaults: { format: "json"}
|
7
|
+
resources :translations, only: [:index, :create, :destroy]
|
8
|
+
|
9
|
+
# necessary to add into your app routes config
|
10
|
+
# mount RedisDictionary::Engine => '/translations'
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "redis"
|
2
|
+
require 'twitter-bootstrap-rails'
|
3
|
+
require 'jquery-rails'
|
4
|
+
require 'jquery-ui-rails'
|
5
|
+
require "redis_dictionary/engine"
|
6
|
+
require "redis_dictionary/configuration"
|
7
|
+
|
8
|
+
module RedisDictionary
|
9
|
+
# Exception raised when gem may not be configured properly
|
10
|
+
class ConfigurationError < StandardError
|
11
|
+
end
|
12
|
+
|
13
|
+
# Set global configuration options for RedisDictionary
|
14
|
+
# @see README.md
|
15
|
+
def self.configure(&block)
|
16
|
+
block.call(configuration)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns RedisDictionary's globalconfiguration. Will initialize a new instance
|
20
|
+
# if not already set
|
21
|
+
def self.configuration
|
22
|
+
@configuration ||= Configuration.new
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module RedisDictionary
|
2
|
+
class Configuration
|
3
|
+
|
4
|
+
# The name of the before filter we'll call to authenticate the current user.
|
5
|
+
# Defaults to :login_required
|
6
|
+
attr_accessor :authentication_method
|
7
|
+
|
8
|
+
# Should the routes of the main app be accessible without
|
9
|
+
# the "main_app." prefix ?
|
10
|
+
attr_accessor :inline_main_app_named_routes
|
11
|
+
|
12
|
+
# Defaults to nil
|
13
|
+
attr_accessor :layout
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@inline_main_app_named_routes = false
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'redis'
|
2
|
+
|
3
|
+
module RedisDictionary
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
isolate_namespace RedisDictionary
|
6
|
+
|
7
|
+
cattr_accessor :redis
|
8
|
+
@@redis = Redis.new
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def load!
|
12
|
+
I18n.backend = I18n::Backend::Simple.new
|
13
|
+
I18n.t('hello')
|
14
|
+
translations = I18n.backend.send(:translations)
|
15
|
+
keys = extract_i18n_keys(translations)
|
16
|
+
|
17
|
+
|
18
|
+
Engine.redis.del('system_keys')
|
19
|
+
to_resave = {}
|
20
|
+
keys.each do |full_key|
|
21
|
+
key = full_key.split('.')[1..-1].join('.')
|
22
|
+
locale = full_key.split('.')[0]
|
23
|
+
Engine.redis.sadd('system_keys', key)
|
24
|
+
to_resave[full_key] = I18n.t(key, locale: locale)
|
25
|
+
end
|
26
|
+
|
27
|
+
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(Engine.redis), I18n.backend)
|
28
|
+
to_resave.each do |full_key, value|
|
29
|
+
key = full_key.split('.')[1..-1].join('.')
|
30
|
+
locale = full_key.split('.')[0]
|
31
|
+
I18n.backend.store_translations(locale, {key => value}, :escape => false)
|
32
|
+
end
|
33
|
+
|
34
|
+
RedisDictionary::Translation.rehash!
|
35
|
+
end
|
36
|
+
|
37
|
+
def extract_i18n_keys(hash, parent_keys = [])
|
38
|
+
hash.inject([]) do |keys, (key, value)|
|
39
|
+
full_key = parent_keys + [key]
|
40
|
+
if value.is_a?(Hash)
|
41
|
+
# Nested hash
|
42
|
+
keys += extract_i18n_keys(value, full_key)
|
43
|
+
elsif value.present?
|
44
|
+
# String leaf node
|
45
|
+
keys << full_key.join(".")
|
46
|
+
end
|
47
|
+
keys
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
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.
|