extend_at 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/.rspec +1 -0
- data/extend_at.gemspec +2 -0
- data/lib/extend_at/model_manager.rb +14 -10
- data/lib/extend_at/version.rb +1 -1
- data/lib/extend_at.rb +18 -25
- data/lib/generators/extend_at/install_generator.rb +0 -6
- data/spec/app/.gitignore +5 -0
- data/spec/app/Gemfile +38 -0
- data/spec/app/Gemfile.lock +136 -0
- data/spec/app/README +261 -0
- data/spec/app/Rakefile +7 -0
- data/spec/app/app/assets/images/rails.png +0 -0
- data/spec/app/app/assets/javascripts/application.js +9 -0
- data/spec/app/app/assets/javascripts/articles.js.coffee +3 -0
- data/spec/app/app/assets/javascripts/users.js.coffee +3 -0
- data/spec/app/app/assets/stylesheets/application.css +7 -0
- data/spec/app/app/assets/stylesheets/articles.css.scss +3 -0
- data/spec/app/app/assets/stylesheets/scaffolds.css.scss +56 -0
- data/spec/app/app/assets/stylesheets/users.css.scss +3 -0
- data/spec/app/app/controllers/application_controller.rb +3 -0
- data/spec/app/app/controllers/articles_controller.rb +83 -0
- data/spec/app/app/controllers/users_controller.rb +83 -0
- data/spec/app/app/helpers/application_helper.rb +2 -0
- data/spec/app/app/helpers/articles_helper.rb +2 -0
- data/spec/app/app/helpers/users_helper.rb +2 -0
- data/spec/app/app/mailers/.gitkeep +0 -0
- data/spec/app/app/models/.gitkeep +0 -0
- data/spec/app/app/models/article.rb +29 -0
- data/spec/app/app/models/user.rb +16 -0
- data/spec/app/app/views/articles/_form.html.erb +29 -0
- data/spec/app/app/views/articles/edit.html.erb +6 -0
- data/spec/app/app/views/articles/index.html.erb +27 -0
- data/spec/app/app/views/articles/new.html.erb +5 -0
- data/spec/app/app/views/articles/show.html.erb +20 -0
- data/spec/app/app/views/layouts/application.html.erb +14 -0
- data/spec/app/app/views/users/_form.html.erb +33 -0
- data/spec/app/app/views/users/edit.html.erb +6 -0
- data/spec/app/app/views/users/index.html.erb +29 -0
- data/spec/app/app/views/users/new.html.erb +5 -0
- data/spec/app/app/views/users/show.html.erb +25 -0
- data/spec/app/config/application.rb +54 -0
- data/spec/app/config/boot.rb +6 -0
- data/spec/app/config/database.yml +25 -0
- data/spec/app/config/environment.rb +5 -0
- data/spec/app/config/environments/development.rb +30 -0
- data/spec/app/config/environments/production.rb +60 -0
- data/spec/app/config/environments/test.rb +42 -0
- data/spec/app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/config/initializers/inflections.rb +10 -0
- data/spec/app/config/initializers/mime_types.rb +5 -0
- data/spec/app/config/initializers/secret_token.rb +7 -0
- data/spec/app/config/initializers/session_store.rb +8 -0
- data/spec/app/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/config/locales/en.yml +5 -0
- data/spec/app/config/routes.rb +62 -0
- data/spec/app/config.ru +4 -0
- data/spec/app/db/migrate/20120101231517_create_articles.rb +11 -0
- data/spec/app/db/migrate/20120104223248_create_users.rb +12 -0
- data/spec/app/db/migrate/20120110153027_create_extend_at_tables.rb +102 -0
- data/spec/app/db/schema.rb +116 -0
- data/spec/app/db/seeds.rb +7 -0
- data/spec/app/doc/README_FOR_APP +2 -0
- data/spec/app/lib/assets/.gitkeep +0 -0
- data/spec/app/lib/tasks/.gitkeep +0 -0
- data/spec/app/log/.gitkeep +0 -0
- data/spec/app/public/404.html +26 -0
- data/spec/app/public/422.html +26 -0
- data/spec/app/public/500.html +26 -0
- data/spec/app/public/favicon.ico +0 -0
- data/spec/app/public/index.html +241 -0
- data/spec/app/public/robots.txt +5 -0
- data/spec/app/script/rails +6 -0
- data/spec/app/test/fixtures/.gitkeep +0 -0
- data/spec/app/test/fixtures/articles.yml +11 -0
- data/spec/app/test/fixtures/users.yml +13 -0
- data/spec/app/test/functional/.gitkeep +0 -0
- data/spec/app/test/functional/articles_controller_test.rb +49 -0
- data/spec/app/test/functional/users_controller_test.rb +49 -0
- data/spec/app/test/integration/.gitkeep +0 -0
- data/spec/app/test/performance/browsing_test.rb +12 -0
- data/spec/app/test/test_helper.rb +13 -0
- data/spec/app/test/unit/.gitkeep +0 -0
- data/spec/app/test/unit/article_test.rb +7 -0
- data/spec/app/test/unit/helpers/articles_helper_test.rb +4 -0
- data/spec/app/test/unit/helpers/users_helper_test.rb +4 -0
- data/spec/app/test/unit/user_test.rb +7 -0
- data/spec/app/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/app/vendor/plugins/.gitkeep +0 -0
- data/spec/extend_at_spec.rb +60 -0
- data/spec/spec_helper.rb +24 -0
- metadata +189 -9
data/.gitignore
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
.directory
|
2
|
-
*~
|
2
|
+
*~
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/extend_at.gemspec
CHANGED
@@ -17,38 +17,42 @@ module ExtendModelAt
|
|
17
17
|
raise "#{value} is not valid" if not @model.send(:valid_type?, value, @config[column.to_sym].try(:[], :type))
|
18
18
|
|
19
19
|
last_model = get_column_model column
|
20
|
-
type_class = get_type_class @config[column.to_sym].try(:[], :type)
|
20
|
+
type_class = get_type_class( (@config[column.to_sym].try(:[], :type) || :any) )
|
21
|
+
|
21
22
|
|
22
23
|
if last_model.nil?
|
23
|
-
eval "
|
24
24
|
new_column = Column.new :extend_at_id => @extend_at.id
|
25
25
|
new_column.save
|
26
|
-
new_value = #{type_class}.new(:column => column, :value => value, :extend_at_column_id => new_column.id)
|
26
|
+
new_value = eval "#{type_class}.new(:column => column, :value => value, :extend_at_column_id => new_column.id)"
|
27
27
|
new_value.save
|
28
28
|
new_column.column_id = new_value.id
|
29
29
|
new_column.column_type = new_value.class.name
|
30
30
|
new_column.save
|
31
|
-
"
|
32
31
|
else
|
33
|
-
|
34
|
-
last_model.save
|
32
|
+
last_model.value = value
|
33
|
+
last_model.save
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
38
37
|
def get_value(column)
|
39
38
|
model = get_column_model column #, get_type(column)
|
40
|
-
model.try(:value)
|
39
|
+
value = model.try(:value)
|
40
|
+
if value.nil?
|
41
|
+
value = @config[column.to_sym].try(:[], :default)
|
42
|
+
assign column, value
|
43
|
+
end
|
44
|
+
value
|
41
45
|
end
|
42
46
|
|
43
47
|
def each()
|
44
48
|
array = []
|
45
49
|
if yield.parameters.size == 1
|
46
50
|
all_values.each do |value|
|
47
|
-
array << yield
|
51
|
+
array << yield(value)
|
48
52
|
end
|
49
53
|
elsif yield.parameters.size == 2
|
50
54
|
all_hash.each do |key, value|
|
51
|
-
array << yield
|
55
|
+
array << yield(key, value)
|
52
56
|
end
|
53
57
|
else
|
54
58
|
raise "Invalid numbers of parameters"
|
@@ -100,7 +104,7 @@ module ExtendModelAt
|
|
100
104
|
if @config[column.to_sym].kind_of? Hash
|
101
105
|
@config[column.to_sym][:type]
|
102
106
|
else
|
103
|
-
|
107
|
+
:any
|
104
108
|
end
|
105
109
|
end
|
106
110
|
|
data/lib/extend_at/version.rb
CHANGED
data/lib/extend_at.rb
CHANGED
@@ -19,18 +19,7 @@ module ExtendModelAt
|
|
19
19
|
|
20
20
|
raise "#{@column_name} should by text or string not #{options[:model].column_for_attribute(@column_name.to_sym).type}" if not [:text, :stiring].include? options[:model].column_for_attribute(@column_name.to_sym).type
|
21
21
|
|
22
|
-
out = YAML.parse(@model[@column_name].to_s)
|
23
|
-
if out == false
|
24
|
-
db_value = nil
|
25
|
-
else
|
26
|
-
db_value = out.to_ruby
|
27
|
-
end
|
28
|
-
@value.merge! db_value if db_value.kind_of? Hash
|
29
|
-
|
30
22
|
initialize_values
|
31
|
-
|
32
|
-
|
33
|
-
@model.attributes[@column_name] = @value
|
34
23
|
end
|
35
24
|
|
36
25
|
def [](key)
|
@@ -224,34 +213,33 @@ module ExtendModelAt
|
|
224
213
|
eval assign_attributes_eval
|
225
214
|
EOS
|
226
215
|
|
227
|
-
class_eval
|
216
|
+
class_eval do
|
228
217
|
public
|
229
218
|
validate :extend_at_validations
|
230
219
|
after_save :update_model_manager, :on => :create
|
231
|
-
|
232
|
-
|
233
|
-
if not
|
234
|
-
opts = initialize_options(
|
220
|
+
|
221
|
+
define_method(column_name.to_s) do
|
222
|
+
if not @extend_at_configuration.kind_of? ExtendModelAt::Extention
|
223
|
+
opts = initialize_options(options)
|
235
224
|
options = {
|
236
225
|
:extensible => true # If is false, only the columns defined in :columns can be used
|
237
226
|
}.merge!(opts)
|
238
227
|
columns = initialize_columns expand_options(options, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] }) if options.kind_of? Hash
|
239
|
-
|
228
|
+
@extend_at_configuration ||= ExtendModelAt::Extention.new({:model => self, :column_name => column_name.to_sym, :columns => columns})
|
240
229
|
end
|
241
|
-
|
230
|
+
@extend_at_configuration
|
242
231
|
end
|
243
232
|
|
244
233
|
protected
|
245
|
-
VALID_SYMBOLS = [:any, :binary, :boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp]
|
246
234
|
|
247
235
|
def extend_at_validations
|
248
|
-
|
236
|
+
# @extend_at_configuration.valid?
|
249
237
|
@extend_at_validation ||= {} if not @extend_at_validation.kind_of? Hash
|
250
238
|
@extend_at_validation.each do |column, validation|
|
251
239
|
if validation.kind_of? Symbol
|
252
|
-
self.send validation, eval("
|
240
|
+
self.send validation, eval("@extend_at_configuration.\#\{column.to_s\}", binding)
|
253
241
|
elsif validation.kind_of? Proc
|
254
|
-
validation.call
|
242
|
+
validation.call @extend_at_configuration[column.to_sym]
|
255
243
|
end
|
256
244
|
end
|
257
245
|
end
|
@@ -281,6 +269,9 @@ module ExtendModelAt
|
|
281
269
|
|
282
270
|
def initialize_column(column,config={})
|
283
271
|
raise "The column \#\{column\} have an invalid configuration (\#\{config.class\} => \#\{config\})" if not config.kind_of? Hash
|
272
|
+
|
273
|
+
@VALID_SYMBOLS ||= [:any, :binary, :boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp]
|
274
|
+
|
284
275
|
column = column.to_sym
|
285
276
|
column_config = {}
|
286
277
|
|
@@ -288,8 +279,10 @@ module ExtendModelAt
|
|
288
279
|
if config[:type].class == Class
|
289
280
|
# If exist :type, is a static column
|
290
281
|
column_config[:type] = get_type_for_class config[:type]
|
291
|
-
elsif config[:type].class == Symbol and VALID_SYMBOLS.include? config[:type]
|
282
|
+
elsif config[:type].class == Symbol and @VALID_SYMBOLS.include? config[:type]
|
292
283
|
column_config[:type] = config[:type]
|
284
|
+
elsif [Symbol, Proc].include? config[:type]
|
285
|
+
column_config[:type] = get_value_of config[:type]
|
293
286
|
else
|
294
287
|
raise "\#\{config[:type]\} is not a valid column type"
|
295
288
|
end
|
@@ -385,7 +378,7 @@ module ExtendModelAt
|
|
385
378
|
end
|
386
379
|
|
387
380
|
def update_model_manager
|
388
|
-
|
381
|
+
@extend_at_configuration.send :update_model_manager
|
389
382
|
end
|
390
383
|
|
391
384
|
def get_type_for_class(type)
|
@@ -418,7 +411,7 @@ module ExtendModelAt
|
|
418
411
|
(type == :boolean and ([true.class, false.class].include? value.class)) or
|
419
412
|
((not [:boolean, nil].include?(type)) and not value.nil? and compatible_type(value, type))
|
420
413
|
end
|
421
|
-
|
414
|
+
end
|
422
415
|
end
|
423
416
|
end
|
424
417
|
end
|
@@ -11,12 +11,6 @@ class ExtendAt::InstallGenerator < Rails::Generators::Base
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def create_model_file
|
14
|
-
# template "models/integer_value.rb", "app/models/integer_value.rb"
|
15
|
-
# template "models/float_value.rb", "app/models/float_value.rb"
|
16
|
-
# template "models/string_value.rb", "app/models/string_value.rb"
|
17
|
-
# template "models/text_value.rb", "app/models/text_value.rb"
|
18
|
-
# template "models/any_value.rb", "app/models/any_value.rb"
|
19
|
-
# template "models/column.rb", "app/models/column.rb"
|
20
14
|
migration_template "create_extend_at_tables.rb", "db/migrate/create_extend_at_tables.rb"
|
21
15
|
end
|
22
16
|
end
|
data/spec/app/.gitignore
ADDED
data/spec/app/Gemfile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '~> 3.1'
|
4
|
+
|
5
|
+
# Bundle edge Rails instead:
|
6
|
+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
+
|
8
|
+
gem 'sqlite3'
|
9
|
+
|
10
|
+
|
11
|
+
# Gems used only for assets and not required
|
12
|
+
# in production environments by default.
|
13
|
+
group :assets do
|
14
|
+
gem 'sass-rails', " ~> 3.1"
|
15
|
+
gem 'coffee-rails', "~> 3.1"
|
16
|
+
gem 'uglifier'
|
17
|
+
end
|
18
|
+
|
19
|
+
gem 'jquery-rails'
|
20
|
+
|
21
|
+
# Use unicorn as the web server
|
22
|
+
# gem 'unicorn'
|
23
|
+
|
24
|
+
# Deploy with Capistrano
|
25
|
+
# gem 'capistrano'
|
26
|
+
|
27
|
+
# To use debugger
|
28
|
+
# gem 'ruby-debug19', :require => 'ruby-debug'
|
29
|
+
|
30
|
+
group :test do
|
31
|
+
# Pretty printed test output
|
32
|
+
gem 'turn', :require => false
|
33
|
+
gem 'rspec'
|
34
|
+
gem 'rspec-core'
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
gem 'extend_at', :path => '../../'
|
@@ -0,0 +1,136 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../../
|
3
|
+
specs:
|
4
|
+
extend_at (0.1.4)
|
5
|
+
rails (~> 3.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (3.1.3)
|
11
|
+
actionpack (= 3.1.3)
|
12
|
+
mail (~> 2.3.0)
|
13
|
+
actionpack (3.1.3)
|
14
|
+
activemodel (= 3.1.3)
|
15
|
+
activesupport (= 3.1.3)
|
16
|
+
builder (~> 3.0.0)
|
17
|
+
erubis (~> 2.7.0)
|
18
|
+
i18n (~> 0.6)
|
19
|
+
rack (~> 1.3.5)
|
20
|
+
rack-cache (~> 1.1)
|
21
|
+
rack-mount (~> 0.8.2)
|
22
|
+
rack-test (~> 0.6.1)
|
23
|
+
sprockets (~> 2.0.3)
|
24
|
+
activemodel (3.1.3)
|
25
|
+
activesupport (= 3.1.3)
|
26
|
+
builder (~> 3.0.0)
|
27
|
+
i18n (~> 0.6)
|
28
|
+
activerecord (3.1.3)
|
29
|
+
activemodel (= 3.1.3)
|
30
|
+
activesupport (= 3.1.3)
|
31
|
+
arel (~> 2.2.1)
|
32
|
+
tzinfo (~> 0.3.29)
|
33
|
+
activeresource (3.1.3)
|
34
|
+
activemodel (= 3.1.3)
|
35
|
+
activesupport (= 3.1.3)
|
36
|
+
activesupport (3.1.3)
|
37
|
+
multi_json (~> 1.0)
|
38
|
+
ansi (1.4.1)
|
39
|
+
arel (2.2.1)
|
40
|
+
builder (3.0.0)
|
41
|
+
coffee-rails (3.1.1)
|
42
|
+
coffee-script (>= 2.2.0)
|
43
|
+
railties (~> 3.1.0)
|
44
|
+
coffee-script (2.2.0)
|
45
|
+
coffee-script-source
|
46
|
+
execjs
|
47
|
+
coffee-script-source (1.2.0)
|
48
|
+
diff-lcs (1.1.3)
|
49
|
+
erubis (2.7.0)
|
50
|
+
execjs (1.2.13)
|
51
|
+
multi_json (~> 1.0)
|
52
|
+
hike (1.2.1)
|
53
|
+
i18n (0.6.0)
|
54
|
+
jquery-rails (1.0.19)
|
55
|
+
railties (~> 3.0)
|
56
|
+
thor (~> 0.14)
|
57
|
+
json (1.6.4)
|
58
|
+
mail (2.3.0)
|
59
|
+
i18n (>= 0.4.0)
|
60
|
+
mime-types (~> 1.16)
|
61
|
+
treetop (~> 1.4.8)
|
62
|
+
mime-types (1.17.2)
|
63
|
+
multi_json (1.0.4)
|
64
|
+
polyglot (0.3.3)
|
65
|
+
rack (1.3.6)
|
66
|
+
rack-cache (1.1)
|
67
|
+
rack (>= 0.4)
|
68
|
+
rack-mount (0.8.3)
|
69
|
+
rack (>= 1.0.0)
|
70
|
+
rack-ssl (1.3.2)
|
71
|
+
rack
|
72
|
+
rack-test (0.6.1)
|
73
|
+
rack (>= 1.0)
|
74
|
+
rails (3.1.3)
|
75
|
+
actionmailer (= 3.1.3)
|
76
|
+
actionpack (= 3.1.3)
|
77
|
+
activerecord (= 3.1.3)
|
78
|
+
activeresource (= 3.1.3)
|
79
|
+
activesupport (= 3.1.3)
|
80
|
+
bundler (~> 1.0)
|
81
|
+
railties (= 3.1.3)
|
82
|
+
railties (3.1.3)
|
83
|
+
actionpack (= 3.1.3)
|
84
|
+
activesupport (= 3.1.3)
|
85
|
+
rack-ssl (~> 1.3.2)
|
86
|
+
rake (>= 0.8.7)
|
87
|
+
rdoc (~> 3.4)
|
88
|
+
thor (~> 0.14.6)
|
89
|
+
rake (0.9.2.2)
|
90
|
+
rdoc (3.12)
|
91
|
+
json (~> 1.4)
|
92
|
+
rspec (2.8.0)
|
93
|
+
rspec-core (~> 2.8.0)
|
94
|
+
rspec-expectations (~> 2.8.0)
|
95
|
+
rspec-mocks (~> 2.8.0)
|
96
|
+
rspec-core (2.8.0)
|
97
|
+
rspec-expectations (2.8.0)
|
98
|
+
diff-lcs (~> 1.1.2)
|
99
|
+
rspec-mocks (2.8.0)
|
100
|
+
sass (3.1.12)
|
101
|
+
sass-rails (3.1.5)
|
102
|
+
actionpack (~> 3.1.0)
|
103
|
+
railties (~> 3.1.0)
|
104
|
+
sass (~> 3.1.10)
|
105
|
+
tilt (~> 1.3.2)
|
106
|
+
sprockets (2.0.3)
|
107
|
+
hike (~> 1.2)
|
108
|
+
rack (~> 1.0)
|
109
|
+
tilt (~> 1.1, != 1.3.0)
|
110
|
+
sqlite3 (1.3.5)
|
111
|
+
thor (0.14.6)
|
112
|
+
tilt (1.3.3)
|
113
|
+
treetop (1.4.10)
|
114
|
+
polyglot
|
115
|
+
polyglot (>= 0.3.1)
|
116
|
+
turn (0.8.3)
|
117
|
+
ansi
|
118
|
+
tzinfo (0.3.31)
|
119
|
+
uglifier (1.2.1)
|
120
|
+
execjs (>= 0.3.0)
|
121
|
+
multi_json (>= 1.0.2)
|
122
|
+
|
123
|
+
PLATFORMS
|
124
|
+
ruby
|
125
|
+
|
126
|
+
DEPENDENCIES
|
127
|
+
coffee-rails (~> 3.1)
|
128
|
+
extend_at!
|
129
|
+
jquery-rails
|
130
|
+
rails (~> 3.1)
|
131
|
+
rspec
|
132
|
+
rspec-core
|
133
|
+
sass-rails (~> 3.1)
|
134
|
+
sqlite3
|
135
|
+
turn
|
136
|
+
uglifier
|
data/spec/app/README
ADDED
@@ -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/spec/app/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
|
+
App::Application.load_tasks
|
Binary file
|
@@ -0,0 +1,9 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into including all the files listed below.
|
2
|
+
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
|
3
|
+
// be included in the compiled file accessible from http://example.com/assets/application.js
|
4
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
5
|
+
// the compiled file.
|
6
|
+
//
|
7
|
+
//= require jquery
|
8
|
+
//= require jquery_ujs
|
9
|
+
//= require_tree .
|
@@ -0,0 +1,7 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll automatically include all the stylesheets available in this directory
|
3
|
+
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
|
4
|
+
* the top of the compiled file, but it's generally better to create a new file per style scope.
|
5
|
+
*= require_self
|
6
|
+
*= require_tree .
|
7
|
+
*/
|