robot_catcher 0.0.1 → 0.0.3

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.
Files changed (95) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +116 -0
  3. data/Rakefile +9 -5
  4. data/lib/robot_catcher/helpers.rb +5 -15
  5. data/lib/robot_catcher/models.rb +9 -5
  6. data/lib/robot_catcher/version.rb +1 -1
  7. data/test/dummy/README.rdoc +248 -15
  8. data/test/dummy/Rakefile +2 -1
  9. data/test/dummy/app/assets/javascripts/application.js +6 -4
  10. data/test/dummy/app/assets/stylesheets/application.css +4 -6
  11. data/test/dummy/app/controllers/application_controller.rb +1 -3
  12. data/test/dummy/app/controllers/articles_controller.rb +46 -12
  13. data/test/dummy/app/models/article.rb +1 -0
  14. data/test/dummy/app/views/articles/_form.html.erb +6 -6
  15. data/test/dummy/app/views/layouts/application.html.erb +2 -2
  16. data/test/dummy/config/application.rb +35 -2
  17. data/test/dummy/config/boot.rb +9 -4
  18. data/test/dummy/config/database.yml +8 -8
  19. data/test/dummy/config/environment.rb +3 -3
  20. data/test/dummy/config/environments/development.rb +18 -22
  21. data/test/dummy/config/environments/production.rb +34 -46
  22. data/test/dummy/config/environments/test.rb +14 -19
  23. data/test/dummy/config/initializers/inflections.rb +5 -6
  24. data/test/dummy/config/initializers/mime_types.rb +1 -0
  25. data/test/dummy/config/initializers/secret_token.rb +7 -0
  26. data/test/dummy/config/initializers/session_store.rb +6 -1
  27. data/test/dummy/config/initializers/wrap_parameters.rb +6 -6
  28. data/test/dummy/config/locales/en.yml +2 -20
  29. data/test/dummy/config/routes.rb +26 -24
  30. data/test/dummy/config.ru +2 -2
  31. data/test/dummy/db/development.sqlite3 +0 -0
  32. data/test/dummy/db/migrate/{20151108205618_create_articles.rb → 0_create_articles.rb} +1 -1
  33. data/test/dummy/db/schema.rb +5 -5
  34. data/test/dummy/db/test.sqlite3 +0 -0
  35. data/test/dummy/log/development.log +1509 -2682
  36. data/test/dummy/log/localdev.log +1 -0
  37. data/test/dummy/public/404.html +13 -54
  38. data/test/dummy/public/422.html +13 -54
  39. data/test/dummy/public/500.html +12 -53
  40. data/test/dummy/script/rails +6 -0
  41. data/test/dummy/tmp/cache/assets/C5B/380/sprockets%2F44f4979327d799d2103258cce2e11554 +0 -0
  42. data/test/dummy/tmp/cache/assets/CA4/AC0/sprockets%2F0069c03442e13e5a34e633c34fbf1866 +0 -0
  43. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  44. data/test/dummy/tmp/cache/assets/CEE/3C0/sprockets%2F39dfc00887914e7b8e1c73859aed2540 +0 -0
  45. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  46. data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  47. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  48. data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  49. data/test/dummy/tmp/cache/assets/E01/2C0/sprockets%2Fb0070cf61ab50c99ce9ddbf2f293d9fe +0 -0
  50. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  51. data/test/dummy/tmp/pids/server.pid +1 -1
  52. data/test/test_helper.rb +3 -7
  53. metadata +63 -132
  54. data/test/dummy/app/assets/javascripts/articles.js +0 -2
  55. data/test/dummy/app/assets/stylesheets/articles.css +0 -4
  56. data/test/dummy/app/assets/stylesheets/scaffold.css +0 -56
  57. data/test/dummy/bin/bundle +0 -3
  58. data/test/dummy/bin/rails +0 -4
  59. data/test/dummy/bin/rake +0 -4
  60. data/test/dummy/bin/setup +0 -29
  61. data/test/dummy/config/initializers/assets.rb +0 -11
  62. data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  63. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  64. data/test/dummy/config/secrets.yml +0 -22
  65. data/test/dummy/test/controllers/articles_controller_test.rb +0 -49
  66. data/test/dummy/test/fixtures/articles.yml +0 -9
  67. data/test/dummy/test/models/article_test.rb +0 -7
  68. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/--2G_c3lCtePsllqkgTSDvk0ws9adbGDQcRvLN_EQ88.cache +0 -1
  69. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/-OJUBxdS-bG4zgi-HNiw9Xlp2QIvHoW8uLDPD75uXpI.cache +0 -2
  70. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/5Lly_CA8DZvPhQV2jDQx-Y6P_y3Ygra9t5jfSlGhHDA.cache +0 -2
  71. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/6uLqoeIeLD2NhPQtj3DYvLFYpEraf1qU147qnysVO6g.cache +0 -1
  72. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/9DpUR_fC14S52b3zrRiKeYjtEArvbqKqmYmCMGHiD9k.cache +0 -0
  73. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/DmmfrCpXtt74Hr6NO54lxyOCDv6klnDyBqeDFR7oDU8.cache +0 -2
  74. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/ELhioJOkik-XcPi3xdXhtHPxP_N8Z1sOLprd6_6TgLY.cache +0 -0
  75. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/JOBnsEqvVtSYuP7X3CVBY5Dxm8D_TINDaEumd2pwmkE.cache +0 -1
  76. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/OI6uxGcnsKavdWTtwDAasU3wPx8QXhzBgV0X2n1KjMQ.cache +0 -2
  77. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/UV1QownSHK65LSLEBMZmSb9Gi-dyrL7sk8uw0slVYEc.cache +0 -1
  78. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/VWPIp3E8NJGBTg9eyT0mbVqhCyhZ1UQzQhIde-3joig.cache +0 -0
  79. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/WQeBW4dfEsVuZ8eNRzY3TghX-GvNZYZy_VCJ42fqMM4.cache +0 -1
  80. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/_xcIdFV-At2pOKyIP31prJqJzCsmdsuoYlkC1DB2jDQ.cache +0 -2
  81. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/aS4gFox4Lh7GJYDZIrtGDgdpp56SlZYzEOdAVy6iSC0.cache +0 -1
  82. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/dphXxQV1ZyMzvKy0tXw9loofG0G8_4MC_ZXSE_eHnFI.cache +0 -2
  83. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/fkqQtAYuWFbb3cJ_DYZg-rtZpbHzWUyEut5YrxkEBOI.cache +0 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/hZi1k6tpxxCGYxRe7zY74ItcOI8gZrREOpGuA8JSpGg.cache +0 -2
  85. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/mLUBzNlQGEI7HKRueSOU69pwu4qJb0dgVLB8yjE6Kr4.cache +0 -1
  86. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/mibJePh9DmzCatUn-6ukNUFR9SygbjuaunJ9JPtwMD0.cache +0 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/pEhaat2KBd5SrT7szC_8R1_6hK17FTpvoRFkmCRSD3M.cache +0 -2
  88. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/pyh8QRGkl9emfRSb4iUUwm_Bta21AwFIzZKueTu30W4.cache +0 -0
  89. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/qvll1qPabMn6TTkpuGOnx99Zg9gsM7t8pp2T63wFYKg.cache +0 -0
  90. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/rNpn7wAAWvFg9ecDT_imL7EYAYHTO17CUnJLmprWzdg.cache +0 -1
  91. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/v5edzu0SjVPbdCoXthlLmNhOVlForWZxZFcuObvVPzk.cache +0 -1
  92. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/wG_whueNWUbRh4wvgMhX2f7EtoDSqX1y_OwF9RgjLvc.cache +0 -2
  93. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/woubQ3T_RRQDsMosM6-mmlJMMsxmT-w16njeEdHzjG4.cache +0 -1
  94. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/zLqPuG6vCF0XaKppCo40WXOlrM9beqArYTWlSUS9lp4.cache +0 -1
  95. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/zNDIuWl4KLiHQ06qzTYjxaWnLh0wa5ePZnqir-PW_KQ.cache +0 -1
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzZmYzkxZWVhZTdhNWRiMmY3YWYwNDAzMzU5OTY1MDhjNWU1ZGVjOQ==
5
+ data.tar.gz: !binary |-
6
+ ZDAyMzlmMmViNWI5ZjZlOGViZjJlZmI5MDc5OWJkZGQ3NjFkYzM4OA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NTkzNjIzOWIyZjU2M2Y4YjdmYzZiOThmMjkyMTcwZDY2OTAwNGMxYzk5N2I2
10
+ MDA5MGRjN2E3YzUxOTg1ZDdkZWFhOTQ1YzQ3YjBmNTAxMTJhZjdlNGE5MzQy
11
+ YzdiM2UxNmM1MmZjNTMzNTJlNDBlNDQxMzQ3NjI1NGE4ZTNkOWY=
12
+ data.tar.gz: !binary |-
13
+ ZTg4MTUyOGYwMjM0ZjQ4ODIyMWY0NTI5YmQ0ZjQ3MzNiZGY2ZGJhNTRiYjNi
14
+ MjI5OTA1YjFjNDZkMTBmMThlZTY0NzBlY2JkMjMwODU4ZDkzMjYxODM0OWIw
15
+ NGY0ZjhlZDRkNjg1MjY1ZWEzNjkzODQ1MDU3M2ZkNTI4ZmZiNTA=
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # RobotCatcher
2
+
3
+ RobotCatcher makes your forms smarter and keeps bots away.
4
+
5
+ ## INSTALLATION
6
+
7
+ Prerequisites:
8
+ + Ruby (built with for example rbenv and ruby-build)
9
+ + Bundle
10
+ + A Rails application (Rails 3.2.22)
11
+
12
+ Installation is simple! Add this line to your application's Gemfile:
13
+
14
+ gem 'robot_catcher'
15
+
16
+ Or in your gem specification file:
17
+
18
+ Gem::Specification.new do |s|
19
+ ...
20
+ s.add_dependency 'robot_catcher'
21
+
22
+ After running `bundle install`, RobotCatcher's functionality will be available in your
23
+ project.
24
+
25
+ ## DESCRIPTION
26
+
27
+ **RobotCatcher** is a small form plugin that uses several kinds of hashing to
28
+ consider whether a form has been filled out by a bot or a human being.
29
+
30
+ ## HOW TO USE
31
+
32
+ ### Front-end (in your .html.erb document)
33
+
34
+ In any forms in your Rails application. Instead of `form_for`, use `rc_form_for`,
35
+ and specify the request ip address as an argument (typically `request.remote_ip`:
36
+
37
+ rc_form_for(@article, ip: request.remote_ip) do |f|
38
+ f.label 'title'
39
+ f.text_field 'title'
40
+
41
+ f.label 'text'
42
+ f.text_area 'text'
43
+
44
+ f.submit
45
+ end
46
+
47
+ Idem dito in case you want to use `form_tag` or anyother field `*_tag`:
48
+
49
+ rc_form_tag('/articles', ip: request.remote_ip) do
50
+ label_tag 'title'
51
+ rc_text_field_tag 'title'
52
+
53
+ label_tag 'text'
54
+ rc_text_area_tag 'text'
55
+
56
+ submit_tag
57
+ end
58
+
59
+ NB: In case of tags, only use tags helper methods on fields (i.e. not on
60
+ labels or submit).
61
+
62
+ ### Back-end
63
+
64
+ #### In the model
65
+
66
+ To specify the attributes that should be investigated by the robot catcher,
67
+ use the class method `robot_catch`. See the example below. A prerequisite
68
+ is that the model class is a child of ActiveRecord::Base.
69
+
70
+ class Article < ActiveRecord::Base
71
+ robot_catch :title, :text
72
+ ...
73
+ end
74
+
75
+ #### In the controller
76
+
77
+ In the current version, catching robots is done by using the `robot?` method on
78
+ a model object. The arguments are the **original** params hash
79
+ (technically a ActionController::Parameters instance), and the request ip address.
80
+ It be the original params hash because that hash contains the information (i.e.
81
+ spinner, timestamp, etc.) that the robot catcher checks. See the example below.
82
+
83
+ def create
84
+ @article = Article.new(params[:article])
85
+
86
+ if @article.robot?(params, request.remote_ip)
87
+ redirect_to @article, notice: 'Stupid robot.'
88
+ return
89
+ end
90
+
91
+ if @article.save
92
+ redirect_to @article, notice: 'Article was successfully created.'
93
+ else
94
+ render :new
95
+ end
96
+ end
97
+
98
+ NB: This requires some knowledge about the difference between what `rc_form_for`
99
+ and `rc_form_tag` generates. As for in the example given in the front-end section
100
+ with `rc_form_tag`, the params hash will include no `:article` key, so a new
101
+ article object is made by passing in the *raw* `params` hash (since this contains
102
+ the right attribute keys and values). Anyway, if you want to do it the Rails way,
103
+ use `rc_form_for` like in the example above.
104
+
105
+ ## UPCOMING NEXT
106
+ + Store robot catches in database
107
+ + Configuration (e.g. for a secret that is used for hashing)
108
+ + More [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself "Don't Repeat Yourself")
109
+
110
+ ### To be considered
111
+ + Random order on spinner hasher
112
+ + Support for more tags
113
+ + Integration into native validations
114
+ + Rewriting native form helper class
115
+
116
+ This project uses MIT-LICENSE.
data/Rakefile CHANGED
@@ -1,10 +1,16 @@
1
+ #!/usr/bin/env rake
1
2
  begin
2
3
  require 'bundler/setup'
3
4
  rescue LoadError
4
5
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
6
  end
6
-
7
- require 'rdoc/task'
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
8
14
 
9
15
  RDoc::Task.new(:rdoc) do |rdoc|
10
16
  rdoc.rdoc_dir = 'rdoc'
@@ -17,8 +23,6 @@ end
17
23
 
18
24
 
19
25
 
20
-
21
-
22
26
  Bundler::GemHelper.install_tasks
23
27
 
24
28
  require 'rake/testtask'
@@ -31,4 +35,4 @@ Rake::TestTask.new(:test) do |t|
31
35
  end
32
36
 
33
37
 
34
- task default: :test
38
+ task :default => :test
@@ -3,20 +3,12 @@ module RobotCatcher
3
3
  class FormBuilder < ActionView::Helpers::FormBuilder
4
4
  def initialize(object_name, object, template, options)
5
5
  @spinner = options[:spinner]
6
- if object_name.nil? and object.nil?
7
- @nested_child_index = {}
8
- @template, @options = template, options
9
- @default_options = @options ? @options.slice(:index, :namespace) : {}
10
- @multipart = nil
11
- @index = options[:index] || options[:child_index]
12
- else
13
- super(object_name, object, template, options)
14
- end
6
+ super(object_name, object, template, options, nil)
15
7
  end
16
8
 
17
9
  def self.create_tagged_field(method_name)
18
10
  define_method(method_name) do |label, *args|
19
- if method_name.eql? :label
11
+ if method_name.eql? "label"
20
12
  super(label, *args)
21
13
  else
22
14
  hash_tag = Digest::MD5.hexdigest(label.to_s + @spinner + "robotcatcher")
@@ -114,8 +106,6 @@ module RobotCatcher
114
106
  raise ArgumentError, "should include ip address"
115
107
  end
116
108
  ip = options.delete(:ip)
117
-
118
- options[:builder] = RobotCatcher::Helpers::FormBuilder
119
109
 
120
110
  timestamp = Time.now.to_i.to_s
121
111
  # p "To be spinned (front-end): #{timestamp}#{ip}robotcatcher"
@@ -133,8 +123,8 @@ module RobotCatcher
133
123
  else
134
124
  object = record.is_a?(Array) ? record.last : record
135
125
  raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
136
- object_name = options[:as] || model_name_from_record_or_class(object).param_key
137
- apply_form_for_options!(record, object, options)
126
+ object_name = options[:as] || object.class.name.downcase
127
+ apply_form_for_options!(record, options)
138
128
  end
139
129
 
140
130
  html_options[:data] = options.delete(:data) if options.has_key?(:data)
@@ -143,7 +133,7 @@ module RobotCatcher
143
133
  html_options[:enforce_utf8] = options.delete(:enforce_utf8) if options.has_key?(:enforce_utf8)
144
134
  html_options[:authenticity_token] = options.delete(:authenticity_token)
145
135
 
146
- builder = instantiate_builder(object_name, object, options)
136
+ builder = RobotCatcher::Helpers::FormBuilder.new(object_name, object, self, options)
147
137
  content = capture(builder, &block)
148
138
  html_options[:multipart] ||= builder.multipart?
149
139
 
@@ -8,7 +8,7 @@ module RobotCatcher
8
8
 
9
9
  self.class_eval do
10
10
  def robot?(params, ip)
11
- timestamp = params["timestamp"]
11
+ timestamp = params[:timestamp]
12
12
 
13
13
  if timestamp.nil? or \
14
14
  timestamp.to_i < Time.now.to_i - 300 or timestamp.to_i > Time.now.to_i
@@ -16,19 +16,23 @@ module RobotCatcher
16
16
  return true
17
17
  end
18
18
 
19
- spinner = params["spinner"]
19
+ spinner = params[:spinner]
20
20
  # p "To be spinned (back-end): #{timestamp}#{ip}robotcatcher"
21
21
  hash_spinner = Digest::MD5.hexdigest(timestamp + ip.to_s + "robotcatcher")
22
22
 
23
23
  if spinner.nil? or spinner != hash_spinner
24
- # p "Spinner: #{spinner}" is not equal #{hash_spinner}!"
24
+ # p "Spinner: #{spinner} is not equal #{hash_spinner}!"
25
25
  return true
26
26
  end
27
27
 
28
28
  object_name = self.class.name.downcase
29
29
  object_params = params.include?(object_name) && \
30
30
  params[object_name].is_a?(Hash) ? params[object_name] : params
31
-
31
+
32
+ # p object_name
33
+ # p object_params
34
+ # p @@rc_params
35
+
32
36
  to_be_validated = object_params.select {|k,v| @@rc_params.include? k.to_sym}
33
37
 
34
38
  if to_be_validated.length != @@rc_params.length
@@ -39,7 +43,7 @@ module RobotCatcher
39
43
  to_be_validated.each do |k, v|
40
44
  hash = Digest::MD5.hexdigest(k.to_s + hash_spinner + "robotcatcher")
41
45
  if !params.include? hash or !params[hash].empty?
42
- # p "Key hash: #{k}'s hash was wrong!
46
+ # p "Key hash: #{k}'s hash was wrong!"
43
47
  return true
44
48
  end
45
49
  end
@@ -1,3 +1,3 @@
1
1
  module RobotCatcher
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,28 +1,261 @@
1
- == README
1
+ == Welcome to Rails
2
2
 
3
- This README would normally document whatever steps are necessary to get the
4
- application up and running.
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
5
 
6
- Things you may want to cover:
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.
7
13
 
8
- * Ruby version
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.
9
19
 
10
- * System dependencies
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.
11
27
 
12
- * Configuration
13
28
 
14
- * Database creation
29
+ == Getting Started
15
30
 
16
- * Database initialization
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)
17
33
 
18
- * How to run the test suite
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)
19
36
 
20
- * Services (job queues, cache servers, search engines, etc.)
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
21
39
 
22
- * Deployment instructions
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
23
42
 
24
- * ...
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
25
45
 
26
46
 
27
- Please feel free to use a different markup language if you do not plan to run
28
- <tt>rake doc:app</tt>.
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.
data/test/dummy/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
+ #!/usr/bin/env rake
1
2
  # Add your own tasks in files placed in lib/tasks ending in .rake,
2
3
  # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
4
 
4
5
  require File.expand_path('../config/application', __FILE__)
5
6
 
6
- Rails.application.load_tasks
7
+ Dummy::Application.load_tasks
@@ -2,12 +2,14 @@
2
2
  // listed below.
3
3
  //
4
4
  // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
6
  //
7
7
  // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file.
8
+ // the compiled file.
9
9
  //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
12
  //
13
+ //= require jquery
14
+ //= require jquery_ujs
13
15
  //= require_tree .
@@ -3,13 +3,11 @@
3
3
  * listed below.
4
4
  *
5
5
  * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
7
  *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any styles
10
- * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
- * file per style scope.
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
12
10
  *
13
- *= require_tree .
14
11
  *= require_self
12
+ *= require_tree .
15
13
  */
@@ -1,5 +1,3 @@
1
1
  class ApplicationController < ActionController::Base
2
- # Prevent CSRF attacks by raising an exception.
3
- # For APIs, you may want to use :null_session instead.
4
- protect_from_forgery with: :exception
2
+ protect_from_forgery
5
3
  end