sinatra_more 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +69 -1
- data/TODO +2 -1
- data/VERSION +1 -1
- data/lib/sinatra_more.rb +1 -0
- data/lib/sinatra_more/warden_helpers.rb +66 -0
- data/sinatra_more.gemspec +2 -1
- metadata +2 -1
data/README.rdoc
CHANGED
@@ -1,6 +1,74 @@
|
|
1
1
|
= sinatra_more
|
2
2
|
|
3
|
-
|
3
|
+
== Introduction
|
4
|
+
|
5
|
+
This will be a plugin which expand sinatra's capabilities in many ways. Not ready to be used yet!
|
6
|
+
|
7
|
+
Let me expand briefly on what I want to accomplish with this gem. I love sinatra but if I want to use it
|
8
|
+
for any non-trivial application I very quickly miss a lot of the extra tools provided by rails.
|
9
|
+
|
10
|
+
Now the obvious question is, why not just use rails? Well, that would be the easy answer. However, until
|
11
|
+
version 3 comes along, Rails is quite a large framework and I love the spirit of sinatra which acts
|
12
|
+
as a thin wrapper on top of rack allowing middleware to do most of the work and pulling in complexity
|
13
|
+
only as needed.
|
14
|
+
|
15
|
+
My goal with this extension is to maintain the spirit of Sinatra and at the same time create a standard library
|
16
|
+
of tools, helpers and components that will make Sinatra scale to allow for extremely complex apps.
|
17
|
+
|
18
|
+
Here is a small list of what sinatra_more might contain:
|
19
|
+
|
20
|
+
* Lots of generic view helpers (tag, content_tag, ...)
|
21
|
+
* Asset helpers (link_to, image_tag, javascript_include_tag, ...)
|
22
|
+
* Form helpers and form builder support (form_tag, form_for, text_field, ...)
|
23
|
+
* Plug and play components for Warden (authentication), ActiveRecord (datastore) and MongoMapper (datastore)
|
24
|
+
|
25
|
+
Keep in mind, the user will be able to pull in components as necessary and leave out any that are not required.
|
26
|
+
|
27
|
+
Obviously the project isn't ready for primetime yet but please help me brainstorm and
|
28
|
+
fork the project if you have any ideas that will help flesh out this project!
|
29
|
+
|
30
|
+
== Usage
|
31
|
+
|
32
|
+
This extension can be easily registered into any existing sinatra application. You can require
|
33
|
+
different components based on which pieces are useful for your application.
|
34
|
+
|
35
|
+
# app.rb
|
36
|
+
require 'sinatra/base'
|
37
|
+
require 'sinatra_more'
|
38
|
+
|
39
|
+
class Application < Sinatra::Base
|
40
|
+
register SinatraMore
|
41
|
+
register SinatraMore::WardenPlugin
|
42
|
+
# (Not yet) register SinatraMore::MarkupHelpers
|
43
|
+
# (Not yet) register SinatraMore::RenderHelpers
|
44
|
+
# (Not yet) register SinatraMore::FormHelpers
|
45
|
+
end
|
46
|
+
|
47
|
+
This will then allow you to use whichever components that have been registered. A breakdown is below:
|
48
|
+
|
49
|
+
=== MarkupPlugin
|
50
|
+
|
51
|
+
This component provides a great deal of view helpers related to html markup generation.
|
52
|
+
There are helpers for generating tags, forms, links, images, and more. Most of the basic
|
53
|
+
methods should be very familiar to anyone who has used rails view helpers.
|
54
|
+
|
55
|
+
...list methods here...
|
56
|
+
|
57
|
+
=== RenderPlugin
|
58
|
+
|
59
|
+
This component provides a number of rendering helpers for sinatra, making the process
|
60
|
+
of displaying templates far smoother. This plugin also has support for useful additions
|
61
|
+
such as partials (with support for :collection) into the templating system.
|
62
|
+
|
63
|
+
...list methods here...
|
64
|
+
|
65
|
+
=== WardenPlugin
|
66
|
+
|
67
|
+
This component provides out-of-the-box support for Warden authentication. With this
|
68
|
+
plugin registered, warden will be automatically required, configured and helpers will be
|
69
|
+
provided to make interacting with warden dead simple.
|
70
|
+
|
71
|
+
...list methods here...
|
4
72
|
|
5
73
|
== Note on Patches/Pull Requests
|
6
74
|
|
data/TODO
CHANGED
@@ -1,2 +1,3 @@
|
|
1
1
|
* Pull from sinatra-helpers to make erb work (and credit keldredd)
|
2
|
-
- http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/erb/
|
2
|
+
- http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/erb/
|
3
|
+
* Become total warden solution (basically just require warden installed, do everything else)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/sinatra_more.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
|
3
|
+
require File.join(File.dirname(__FILE__) + '/sinatra_more/warden_helpers')
|
3
4
|
require File.join(File.dirname(__FILE__) + '/sinatra_more/view_helpers')
|
4
5
|
Dir.glob(File.dirname(__FILE__) + '/sinatra_more/form_builder/*.rb').each { |f| require f }
|
5
6
|
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module SinatraMore
|
2
|
+
module WardenHelpers
|
3
|
+
# Returns the current authenticated user
|
4
|
+
def current_user
|
5
|
+
warden_handler.user
|
6
|
+
end
|
7
|
+
|
8
|
+
# Login the user through the specified warden strategy
|
9
|
+
def authenticate_user!
|
10
|
+
warden_handler.authenticate!
|
11
|
+
end
|
12
|
+
|
13
|
+
# Signs out the user
|
14
|
+
def logout_user!
|
15
|
+
warden_handler.logout
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns true if the user has authenticated
|
19
|
+
def logged_in?
|
20
|
+
warden_handler.authenticated?
|
21
|
+
end
|
22
|
+
|
23
|
+
# If a block is given, only yields the content if the user is authenticated
|
24
|
+
# If no block is given, returns true if the user is logged in
|
25
|
+
def authenticated?(&block)
|
26
|
+
if block_given?
|
27
|
+
authenticated_content = capture_haml(&block)
|
28
|
+
logged_in? ? haml_concat(authenticated_content) : ''
|
29
|
+
else
|
30
|
+
return logged_in?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Forces a user to return to a fail path unless they are authorized
|
35
|
+
# Used to require a user be authenticated before routing to an action
|
36
|
+
def must_be_authorized!(failure_path=nil)
|
37
|
+
redirect_to(failure_path ? failure_path : '/') unless authenticated?
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns the raw warden authentication handler
|
41
|
+
def warden_handler
|
42
|
+
request.env['warden']
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module WardenPlugin
|
47
|
+
def self.registered(app)
|
48
|
+
app.helpers SinatraMore::WardenHelpers
|
49
|
+
|
50
|
+
# TODO Improve serializing
|
51
|
+
Warden::Manager.serialize_into_session{ |user| user.nil? ? nil : user.id }
|
52
|
+
Warden::Manager.serialize_from_session{ |id| id.nil? ? nil : User.find(id) }
|
53
|
+
|
54
|
+
Warden::Strategies.add(:password) do
|
55
|
+
def valid?
|
56
|
+
params['username'] || params['password']
|
57
|
+
end
|
58
|
+
|
59
|
+
def authenticate!
|
60
|
+
u = User.authenticate(params['username'], params['password'])
|
61
|
+
u.nil? ? fail!("Could not log in") : success!(u)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/sinatra_more.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sinatra_more}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Nathan Esquenazi"]
|
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
"lib/sinatra_more/view_helpers/format_helpers.rb",
|
34
34
|
"lib/sinatra_more/view_helpers/render_helpers.rb",
|
35
35
|
"lib/sinatra_more/view_helpers/tag_helpers.rb",
|
36
|
+
"lib/sinatra_more/warden_helpers.rb",
|
36
37
|
"sinatra_more.gemspec",
|
37
38
|
"test/helper.rb",
|
38
39
|
"test/test_sinatra_more.rb"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra_more
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Esquenazi
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- lib/sinatra_more/view_helpers/format_helpers.rb
|
49
49
|
- lib/sinatra_more/view_helpers/render_helpers.rb
|
50
50
|
- lib/sinatra_more/view_helpers/tag_helpers.rb
|
51
|
+
- lib/sinatra_more/warden_helpers.rb
|
51
52
|
- sinatra_more.gemspec
|
52
53
|
- test/helper.rb
|
53
54
|
- test/test_sinatra_more.rb
|