short_circuit 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +54 -31
  4. data/lib/short_circuit.rb +5 -3
  5. data/lib/short_circuit/presentable.rb +1 -0
  6. data/lib/short_circuit/presenter.rb +8 -7
  7. data/lib/short_circuit/version.rb +1 -1
  8. data/spec/dummy/README.rdoc +15 -248
  9. data/spec/dummy/Rakefile +0 -1
  10. data/spec/dummy/app/assets/javascripts/application.js +3 -5
  11. data/spec/dummy/app/controllers/application_controller.rb +3 -1
  12. data/spec/dummy/app/views/layouts/application.html.erb +2 -2
  13. data/spec/dummy/bin/bundle +3 -0
  14. data/spec/dummy/bin/rails +4 -0
  15. data/spec/dummy/bin/rake +4 -0
  16. data/spec/dummy/config.ru +1 -1
  17. data/spec/dummy/config/application.rb +6 -37
  18. data/spec/dummy/config/boot.rb +4 -9
  19. data/spec/dummy/config/environment.rb +2 -2
  20. data/spec/dummy/config/environments/development.rb +11 -19
  21. data/spec/dummy/config/environments/production.rb +40 -27
  22. data/spec/dummy/config/environments/test.rb +13 -14
  23. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  24. data/spec/dummy/config/initializers/inflections.rb +6 -5
  25. data/spec/dummy/config/initializers/secret_token.rb +7 -2
  26. data/spec/dummy/config/initializers/session_store.rb +0 -5
  27. data/spec/dummy/config/initializers/wrap_parameters.rb +6 -6
  28. data/spec/dummy/config/locales/en.yml +20 -2
  29. data/spec/dummy/config/routes.rb +22 -24
  30. data/spec/dummy/log/test.log +0 -37
  31. data/spec/dummy/public/404.html +43 -11
  32. data/spec/dummy/public/422.html +43 -11
  33. data/spec/dummy/public/500.html +43 -11
  34. data/spec/short_circuit/presentable_spec.rb +53 -16
  35. data/spec/short_circuit/presenter_spec.rb +54 -0
  36. data/spec/spec_helper.rb +9 -17
  37. metadata +19 -52
  38. data/Rakefile +0 -21
  39. data/lib/active_record_extension.rb +0 -13
  40. data/lib/tasks/short_circuit_tasks.rake +0 -4
  41. data/spec/dummy/log/development.log +0 -1
  42. data/spec/dummy/script/rails +0 -6
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82ba9bf7e740eb2c2d50b767e63025893c51c11f
4
+ data.tar.gz: 75510a972b29489923a5fbe7b25285afd57dce0b
5
+ SHA512:
6
+ metadata.gz: 172e84d9ae5b7c5c8965061ab49048e2a16f8266cf65cf38daee8e74d1e045867112b30b2b6242f2192fdcb4473b0b95c615dbe4bf74f3f0ea2f58a0e58a90dc
7
+ data.tar.gz: 48fba1a87f641240f4e8aafb6b07d42d2a01694252b6f3d13b25c5d48010020a526072696f4ba8d0b6fd853a7fc2eb2dce7e39fb7c9510d7c03b64d365e48124
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2013 YOURNAME
1
+ Copyright 2014 Jim Pruetting
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -4,86 +4,109 @@ short_circuit [![Build Status](https://travis-ci.org/jpruetting/short_circuit.pn
4
4
  Short Circuit adds simple presenters for Rails views.
5
5
 
6
6
 
7
- Installation
8
- =============
7
+ Usage
8
+ ======
9
9
 
10
- Include the gem in your Gemfile:
10
+ Install the gem in your Gemfile:
11
11
 
12
12
  ```ruby
13
13
  gem 'short_circuit'
14
14
  ```
15
15
 
16
- Usage
17
- =============
18
-
19
- Model:
16
+ Include short_circuit in your model:
20
17
 
21
18
  ```ruby
19
+ # app/models/user.rb
20
+
22
21
  class User < ActiveRecord::Base
22
+ include ShortCircuit::Presentable
23
+
23
24
  attr_accessible :first_name, :last_name, :job_title, :member_since
24
25
  end
25
26
  ```
26
27
 
27
- Presenter:
28
+ Create a presenter:
28
29
 
29
30
  ```ruby
31
+ # app/presenters/user_presenter.rb
32
+
30
33
  class UserPresenter < ShortCircuit::Presenter
31
34
  def first_name
32
35
  @user.first_name.titleize
33
36
  end
34
-
37
+
35
38
  def full_name
36
39
  "#{first_name} #{last_name}"
37
40
  end
38
-
41
+
39
42
  def job_title
40
43
  @user.job_title || 'not listed'
41
44
  end
42
-
45
+
43
46
  def member_since
44
47
  @user.member_since.to_formatted_s(:long)
45
48
  end
46
-
49
+
47
50
  def error_response(method, *args, &block)
48
51
  link_to 'N/A', root_path
49
52
  end
50
53
  end
51
54
  ```
52
55
 
53
- Examples:
56
+ No additional controller code is necessary. Retrieve the model as you normally would:
54
57
 
55
58
  ```ruby
56
- @user = User.new(
57
- first_name: 'john',
58
- last_name: 'smith',
59
- job_title: nil,
60
- member_since: 3.months.ago
61
- )
59
+ # app/controllers/users_controller.rb
60
+
61
+ class UsersController < ApplicationController
62
+ def show
63
+ @user = User.find(params['id'])
64
+ end
65
+ end
66
+ ```
67
+
68
+ In the view, you don't need to instantiate any presenter/decorator objects, just use the model:
62
69
 
70
+ ```ruby
71
+ # Normal method vs presenter method:
63
72
  @user.first_name # john
64
73
  @user.present :first_name # John
65
-
74
+
75
+
76
+ # Missing presenter method is delegated to model:
66
77
  @user.last_name # smith
67
78
  @user.present :last_name # smith
68
-
79
+
80
+
81
+ # Method defined on presenter object:
69
82
  @user.full_name # NoMethodError
70
83
  @user.present :full_name # John smith
71
-
84
+
85
+
86
+ # Formatted data returned by presenter method:
72
87
  @user.member_since # 2013-02-28 20:46:32 UTC
73
88
  @user.present :member_since # February 28, 2013 20:46
74
-
89
+
90
+
91
+ # Presenter method prevents nil error by returning default data:
75
92
  @user.job_title.upcase # undefined method 'upcase' for nil:NilClass
76
93
  @user.present(:job_title).upcase # NOT LISTED
77
-
94
+
95
+
96
+ # Presenter method silences nil error and returns alternative content:
78
97
  @user.not_a_real_method # NoMethodError
79
98
  @user.present :not_a_real_method # <a href="/">N/A</a>
80
-
99
+
100
+
101
+ # Present! method does not silence errors:
81
102
  @user.present! :not_a_real_method # NoMethodError
82
103
  ```
83
104
 
84
- Design Decisions
85
- =============
86
- * All presenter methods are accessed via 'present' and 'present!'. This helps identify whether the output is coming from the presenter or directly from the model. It also keeps intact the familiar and direct access to a model's attributes.
87
- * The 'present' method will fail silently. This keeps minor accessor errors from breaking an entire page. Errors can still be logged/handled via the 'error_response' method.
88
- * If you do not want a particular method call to fail silently, the 'present!' method will throw errors as usual.
89
- * If a method is not found on the presenter, it will delegate the call to the model.
105
+ Design goals for short_circuit
106
+ ===============================
107
+ * Minimize project size and scope
108
+ * Preserve direct access to model attributes
109
+ * Create a separate access point for presenter methods
110
+ * Integrate with ActiveRecord models by default
111
+ * Silence accessor errors by default
112
+ * Delegate missing presenter methods to the model object
data/lib/short_circuit.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module ShortCircuit
2
- require "short_circuit/version"
3
- require "short_circuit/presenter"
4
- require "active_record_extension"
2
+ require 'active_support/all'
3
+
4
+ require 'short_circuit/version'
5
+ require 'short_circuit/presenter'
6
+ require 'short_circuit/presentable'
5
7
  end
@@ -21,6 +21,7 @@ module ShortCircuit
21
21
  def find_presenter
22
22
  presenter_class_name = "#{self.class.name}Presenter"
23
23
  presenter_class = Object.const_get(presenter_class_name)
24
+
24
25
  presenter_class.new(self)
25
26
  end
26
27
  end
@@ -5,12 +5,9 @@ module ShortCircuit
5
5
  delegate :url_helpers, to: 'Rails.application.routes'
6
6
 
7
7
  def initialize(presentable_object)
8
- instance_variable_set("@#{presentable_object.class.to_s.downcase}", presentable_object)
9
- super(presentable_object)
10
- end
8
+ instance_variable_set("@#{presentable_object.class.to_s.underscore}", presentable_object)
11
9
 
12
- def helpers
13
- ApplicationController.helpers
10
+ super(presentable_object)
14
11
  end
15
12
 
16
13
  def error_response(error, method, *args, &block)
@@ -19,9 +16,13 @@ module ShortCircuit
19
16
 
20
17
  private
21
18
 
19
+ def controller_helpers
20
+ ApplicationController.helpers
21
+ end
22
+
22
23
  def method_missing(method, *args, &block)
23
- if helpers.respond_to?(method)
24
- helpers.send(method, *args, &block)
24
+ if controller_helpers.respond_to?(method)
25
+ controller_helpers.send(method, *args, &block)
25
26
  elsif url_helpers.respond_to?(method)
26
27
  url_helpers.send(method, *args, &block)
27
28
  else
@@ -1,3 +1,3 @@
1
1
  module ShortCircuit
2
- VERSION = "0.1.0"
2
+ VERSION = '1.0.0'
3
3
  end
@@ -1,261 +1,28 @@
1
- == Welcome to Rails
1
+ == README
2
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.
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
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.
6
+ Things you may want to cover:
13
7
 
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.
8
+ * Ruby version
19
9
 
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.
10
+ * System dependencies
27
11
 
12
+ * Configuration
28
13
 
29
- == Getting Started
14
+ * Database creation
30
15
 
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)
16
+ * Database initialization
33
17
 
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)
18
+ * How to run the test suite
36
19
 
37
- 3. Go to http://localhost:3000/ and you'll see:
38
- "Welcome aboard: You're riding Ruby on Rails!"
20
+ * Services (job queues, cache servers, search engines, etc.)
39
21
 
40
- 4. Follow the guidelines to start developing your application. You can find
41
- the following resources handy:
22
+ * Deployment instructions
42
23
 
43
- * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
- * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
24
+ * ...
45
25
 
46
26
 
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.
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.