happy 0.1.0.pre25 → 0.1.0.pre27
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/.yardopts +1 -0
- data/README.md +1 -1
- data/TUTORIAL.md +3 -0
- data/example/config.ru +13 -0
- data/example/views/block_helpers.erb +3 -0
- data/example/views/block_helpers.haml +2 -0
- data/happy.gemspec +1 -1
- data/lib/happy/controller/configurable.rb +14 -6
- data/lib/happy/controller.rb +11 -12
- data/lib/happy/errors.rb +3 -3
- data/lib/happy/extras/active_model_resource_controller.rb +14 -14
- data/lib/happy/extras/scriptable.rb +1 -1
- data/lib/happy/extras/static.rb +1 -1
- data/lib/happy/helpers/html.rb +50 -0
- data/lib/happy/helpers/i18n.rb +13 -0
- data/lib/happy/helpers/rendering.rb +89 -0
- data/lib/happy/helpers.rb +19 -0
- data/lib/happy/request/date_parameter_converter.rb +25 -0
- data/lib/happy/request.rb +11 -3
- data/lib/happy/version.rb +1 -1
- data/lib/happy.rb +8 -0
- data/spec/controller/configurable_spec.rb +12 -12
- data/spec/helpers/html_spec.rb +48 -0
- data/spec/request_spec.rb +22 -0
- metadata +39 -28
- data/.yardopt +0 -1
- data/lib/happy/controller/helpers.rb +0 -52
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--no-private --protected --embed-mixins lib/**/*.rb - README.md TUTORIAL.md LICENSE
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ Happy is available as a RubyGem, so just install it through `gem install happy`
|
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
17
|
-
* The Happy Book of Happy
|
17
|
+
* [The Happy Book of Happy](https://github.com/hmans/happy/blob/master/TUTORIAL.md)
|
18
18
|
* [Reference Documentation](http://rdoc.info/github/hmans/happy/master/)
|
19
19
|
|
20
20
|
## Reporting Bugs & Contributing
|
data/TUTORIAL.md
ADDED
data/example/config.ru
CHANGED
@@ -7,6 +7,8 @@ require 'happy'
|
|
7
7
|
require 'happy/extras/action_controller'
|
8
8
|
require 'happy/extras/resource_controller'
|
9
9
|
|
10
|
+
require 'haml'
|
11
|
+
|
10
12
|
# Controllers are the core building blocks of Happy applications.
|
11
13
|
# They're also just Rack apps, so in any Happy app, you will
|
12
14
|
# declare at least a "root" controller class and run that through Rack.
|
@@ -24,6 +26,8 @@ class TestApp < Happy::Controller
|
|
24
26
|
# method, that does the same thing but also records the example in a hash
|
25
27
|
# so we can generated a "table of contents" in index.erb.
|
26
28
|
|
29
|
+
set :views, File.expand_path("#{File.dirname(__FILE__)}/views")
|
30
|
+
|
27
31
|
def route
|
28
32
|
example 'Returning just a string' do
|
29
33
|
"I'm just a string!"
|
@@ -120,9 +124,18 @@ class TestApp < Happy::Controller
|
|
120
124
|
run ResourceTest
|
121
125
|
end
|
122
126
|
|
127
|
+
example "Block Helpers" do
|
128
|
+
on('erb') { render 'block_helpers.erb' }
|
129
|
+
on('haml') { render 'block_helpers.haml' }
|
130
|
+
end
|
131
|
+
|
123
132
|
render 'index.erb'
|
124
133
|
end
|
125
134
|
|
135
|
+
def blockquotify(&blk)
|
136
|
+
concat_output html_tag(:blockquote) { capture_template_block(&blk) }
|
137
|
+
end
|
138
|
+
|
126
139
|
def examples; @examples ||= {}; end
|
127
140
|
|
128
141
|
def example(name, path_name = nil, &blk)
|
data/happy.gemspec
CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |gem|
|
|
17
17
|
|
18
18
|
gem.add_dependency 'activesupport', '~> 3.1'
|
19
19
|
gem.add_dependency 'rack', '~> 1.4'
|
20
|
-
gem.add_dependency 'happy-helpers', '~> 0.1.0.pre11'
|
21
20
|
gem.add_dependency 'allowance', '>= 0.1.1'
|
21
|
+
gem.add_dependency 'tilt', '~> 1.3'
|
22
22
|
|
23
23
|
# gem.add_dependency 'happy-cli', '>= 0.1.0.pre1'
|
24
24
|
|
@@ -3,21 +3,29 @@ module Happy
|
|
3
3
|
module Configurable
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
# Return a hash containing this controller instance's settings.
|
7
|
+
#
|
8
|
+
def settings
|
9
|
+
@settings ||= self.class.settings.dup
|
8
10
|
end
|
9
11
|
|
12
|
+
# Change a setting on this controller instance.
|
13
|
+
#
|
10
14
|
def set(k, v)
|
11
|
-
|
15
|
+
settings[k.to_sym] = v
|
12
16
|
end
|
13
17
|
|
14
18
|
module ClassMethods
|
15
|
-
|
16
|
-
|
19
|
+
# Return a hash containing this controller class' default settings.
|
20
|
+
#
|
21
|
+
def settings
|
22
|
+
@settings ||= {}
|
17
23
|
end
|
18
24
|
|
25
|
+
# Change a default setting on this controller class.
|
26
|
+
#
|
19
27
|
def set(k, v)
|
20
|
-
|
28
|
+
settings[k.to_sym] = v
|
21
29
|
end
|
22
30
|
end
|
23
31
|
end
|
data/lib/happy/controller.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'happy/request'
|
2
2
|
require 'happy/response'
|
3
3
|
|
4
|
-
require 'happy/controller/helpers'
|
5
4
|
require 'happy/controller/routing'
|
6
5
|
require 'happy/controller/actions'
|
7
6
|
require 'happy/controller/rackable'
|
@@ -22,20 +21,20 @@ module Happy
|
|
22
21
|
include Configurable
|
23
22
|
include Cascadable
|
24
23
|
include Permissions
|
25
|
-
include Helpers
|
24
|
+
include Happy::Helpers
|
26
25
|
|
27
26
|
attr_reader :env
|
28
27
|
|
29
|
-
|
28
|
+
CASCADING_SETTINGS = [:views]
|
30
29
|
|
31
30
|
# Creates a new instance of {Controller}. When a block is provided,
|
32
31
|
# it is run against the new instance, allowing custom controller classes
|
33
32
|
# to provide DSL-like configuration.
|
34
33
|
#
|
35
|
-
# @param
|
36
|
-
# Rack environment hash.
|
37
|
-
# @param
|
38
|
-
#
|
34
|
+
# @param env_or_parent [Hash,Controller]
|
35
|
+
# Rack environment hash _or_ parent controller.
|
36
|
+
# @param opts [Hash]
|
37
|
+
# Options to be merged with the controller's default (class-level) settings.
|
39
38
|
#
|
40
39
|
def initialize(env_or_parent = {}, opts = {}, &blk)
|
41
40
|
if env_or_parent.is_a?(Happy::Controller)
|
@@ -49,13 +48,13 @@ module Happy
|
|
49
48
|
@processed_path = []
|
50
49
|
end
|
51
50
|
|
52
|
-
# Augment this instance's
|
53
|
-
|
51
|
+
# Augment this instance's settings hash with the hash given to this constructor
|
52
|
+
settings.merge!(opts)
|
54
53
|
|
55
|
-
# Copy missing
|
54
|
+
# Copy missing settings from our parent
|
56
55
|
if @parent_controller
|
57
|
-
|
58
|
-
|
56
|
+
CASCADING_SETTINGS.each do |name|
|
57
|
+
settings[name] ||= @parent_controller.settings[name]
|
59
58
|
end
|
60
59
|
end
|
61
60
|
|
data/lib/happy/errors.rb
CHANGED
@@ -5,10 +5,10 @@ module Happy
|
|
5
5
|
|
6
6
|
# Render a HTML page for the given exception.
|
7
7
|
#
|
8
|
-
# @param [Exception]
|
8
|
+
# @param exception [Exception]
|
9
9
|
# The exception to display.
|
10
|
-
# @param [Hash]
|
11
|
-
# The
|
10
|
+
# @param controller [Hash]
|
11
|
+
# The Happy Controller that caught the exception.
|
12
12
|
#
|
13
13
|
# @option options [String] :title
|
14
14
|
# Title of error page
|
@@ -5,39 +5,39 @@ module Happy
|
|
5
5
|
|
6
6
|
class ActiveModelResourceController < Happy::Extras::ResourceController
|
7
7
|
def root_url
|
8
|
-
super(
|
8
|
+
super(settings[:plural_name])
|
9
9
|
end
|
10
10
|
|
11
11
|
def render_resource_template(name)
|
12
|
-
render "#{
|
12
|
+
render "#{settings[:plural_name]}/#{name}.html.haml"
|
13
13
|
end
|
14
14
|
|
15
15
|
def resource
|
16
|
-
|
16
|
+
settings[:class]
|
17
17
|
end
|
18
18
|
|
19
19
|
def resource_with_permission_scope(*args)
|
20
|
-
permissions.scoped_model(*args,
|
20
|
+
permissions.scoped_model(*args, settings[:class])
|
21
21
|
end
|
22
22
|
|
23
23
|
def require_permission!(*args)
|
24
|
-
raise "not allowed" unless permissions.can?(*args,
|
24
|
+
raise "not allowed" unless permissions.can?(*args, settings[:class])
|
25
25
|
end
|
26
26
|
|
27
27
|
def set_plural_variable(v)
|
28
|
-
instance_variable_set "@#{
|
28
|
+
instance_variable_set "@#{settings[:plural_name]}", v
|
29
29
|
end
|
30
30
|
|
31
31
|
def plural_variable
|
32
|
-
instance_variable_get "@#{
|
32
|
+
instance_variable_get "@#{settings[:plural_name]}"
|
33
33
|
end
|
34
34
|
|
35
35
|
def set_singular_variable(v)
|
36
|
-
instance_variable_set "@#{
|
36
|
+
instance_variable_set "@#{settings[:singular_name]}", v
|
37
37
|
end
|
38
38
|
|
39
39
|
def singular_variable
|
40
|
-
instance_variable_get "@#{
|
40
|
+
instance_variable_get "@#{settings[:singular_name]}"
|
41
41
|
end
|
42
42
|
|
43
43
|
def index
|
@@ -54,13 +54,13 @@ module Happy
|
|
54
54
|
|
55
55
|
def new
|
56
56
|
require_permission! :new
|
57
|
-
set_singular_variable resource_with_permission_scope(:new).new(params[
|
57
|
+
set_singular_variable resource_with_permission_scope(:new).new(params[settings[:singular_name]], :as => settings[:role])
|
58
58
|
render_resource_template 'new'
|
59
59
|
end
|
60
60
|
|
61
61
|
def create
|
62
62
|
require_permission! :create
|
63
|
-
set_singular_variable resource_with_permission_scope(:create).new(params[
|
63
|
+
set_singular_variable resource_with_permission_scope(:create).new(params[settings[:singular_name]], :as => settings[:role])
|
64
64
|
|
65
65
|
if singular_variable.save
|
66
66
|
redirect! singular_variable
|
@@ -78,7 +78,7 @@ module Happy
|
|
78
78
|
def update
|
79
79
|
require_permission! :update
|
80
80
|
set_singular_variable resource_with_permission_scope(:update).find(params['id'])
|
81
|
-
singular_variable.assign_attributes params[
|
81
|
+
singular_variable.assign_attributes params[settings[:singular_name]], :as => settings[:role]
|
82
82
|
|
83
83
|
if singular_variable.save
|
84
84
|
redirect! singular_variable
|
@@ -88,8 +88,8 @@ module Happy
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def route
|
91
|
-
|
92
|
-
|
91
|
+
settings[:singular_name] ||= settings[:class].to_s.tableize.singularize
|
92
|
+
settings[:plural_name] ||= settings[:class].to_s.tableize.pluralize
|
93
93
|
|
94
94
|
super
|
95
95
|
end
|
data/lib/happy/extras/static.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'cgi'
|
3
|
+
|
4
|
+
module Happy
|
5
|
+
module Helpers
|
6
|
+
module Html
|
7
|
+
def html_tag(name, options = nil, escape = true, &block)
|
8
|
+
"<#{name} #{html_tag_attributes(options, escape) if options}#{block_given? ? ">#{yield if block_given?}</#{name}>" : " />"}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def html_tag_attributes(options, escape = true)
|
12
|
+
options.map do |k,v|
|
13
|
+
if v
|
14
|
+
v == true ? "#{k}" : "#{k}=\"#{ escape_html(v) }\""
|
15
|
+
end
|
16
|
+
end.compact.join(" ")
|
17
|
+
end
|
18
|
+
|
19
|
+
def escape_html(t)
|
20
|
+
# Rack::Utils.escape_html(t.to_s)
|
21
|
+
CGI::escape_html(t.to_s)
|
22
|
+
end
|
23
|
+
|
24
|
+
def preserve(t)
|
25
|
+
t.chomp("\n").gsub(/\n/, '
').gsub(/\r/, '')
|
26
|
+
end
|
27
|
+
|
28
|
+
def link_to(name, *target)
|
29
|
+
options = target.last.is_a?(Hash) ? target.pop : {}
|
30
|
+
html_tag(:a, options.merge(:href => url_for(*target))) { name }
|
31
|
+
end
|
32
|
+
|
33
|
+
def url_for(*what)
|
34
|
+
return what.first if what.size == 1 && what.first =~ %r{://}
|
35
|
+
|
36
|
+
result = what.flatten.inject('') do |url, item|
|
37
|
+
url << "/%s" % case item
|
38
|
+
when String, Symbol then item.to_s
|
39
|
+
when NilClass then "/"
|
40
|
+
else "%s/%s" % [item.class.to_s.tableize.pluralize, item.try(:to_param) || item.try(:to_id) || item.try(:id)]
|
41
|
+
end
|
42
|
+
|
43
|
+
url
|
44
|
+
end.gsub(/\/{2,}/, '/').chomp('/')
|
45
|
+
|
46
|
+
result == "" ? '/' : result
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
|
3
|
+
module Happy
|
4
|
+
module Helpers
|
5
|
+
module Rendering
|
6
|
+
attr_accessor :output_buffer
|
7
|
+
|
8
|
+
# Renders "something". This method takes a closer look at what this
|
9
|
+
# "something" is and then dispatches to a more specific method.
|
10
|
+
#
|
11
|
+
def render(what, options = {}, &blk)
|
12
|
+
case what
|
13
|
+
when NilClass then ''
|
14
|
+
when String then render_template(what, options, &blk)
|
15
|
+
when Enumerable then what.map { |i| render(i, options, &blk) }.join
|
16
|
+
else render_resource(what, options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Render a template from the controller's view folder.
|
21
|
+
#
|
22
|
+
def render_template(name, variables = {}, &blk)
|
23
|
+
path = settings[:views] || './views'
|
24
|
+
full_name = File.expand_path(File.join(path, name))
|
25
|
+
|
26
|
+
# load and cache template
|
27
|
+
@@cached_templates ||= {}
|
28
|
+
t = @@cached_templates[full_name] =
|
29
|
+
(Happy.env.production? && @@cached_templates[full_name]) ||
|
30
|
+
Tilt.new(full_name, :default_encoding => 'utf-8', :outvar => "@output_buffer")
|
31
|
+
|
32
|
+
# render template
|
33
|
+
t.render(self, variables, &blk)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Render a resource.
|
37
|
+
#
|
38
|
+
def render_resource(resource, options = {})
|
39
|
+
# build name strings
|
40
|
+
singular_name = resource.class.to_s.tableize.singularize
|
41
|
+
plural_name = singular_name.pluralize
|
42
|
+
|
43
|
+
# set options
|
44
|
+
options = {
|
45
|
+
singular_name => resource
|
46
|
+
}.merge(options)
|
47
|
+
|
48
|
+
# render
|
49
|
+
render_template("#{plural_name}/_#{singular_name}.html.haml", options)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# Capture a block from a template. Use this inside view helpers that
|
54
|
+
# take blocks.
|
55
|
+
#
|
56
|
+
def capture_template_block(*args, &blk)
|
57
|
+
if respond_to?(:is_haml?) && is_haml?
|
58
|
+
capture_haml(*args, &blk)
|
59
|
+
else
|
60
|
+
with_output_buffer(&blk)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Add something to the output being rendered by the current template.
|
65
|
+
# Use this inside view helpers that take blocks.
|
66
|
+
#
|
67
|
+
def concat_output(v)
|
68
|
+
if respond_to?(:is_haml?) && is_haml?
|
69
|
+
v
|
70
|
+
else
|
71
|
+
self.output_buffer << v
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Execute the given block, adding its generated output to a new view
|
76
|
+
# buffer, and finally returning that buffer. Use this inside view helpers
|
77
|
+
# that take blocks.
|
78
|
+
#
|
79
|
+
def with_output_buffer
|
80
|
+
self.output_buffer, old_buffer = "", self.output_buffer
|
81
|
+
yield if block_given?
|
82
|
+
output_buffer
|
83
|
+
ensure
|
84
|
+
self.output_buffer = old_buffer
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'happy/helpers/html'
|
2
|
+
require 'happy/helpers/i18n'
|
3
|
+
require 'happy/helpers/rendering'
|
4
|
+
|
5
|
+
module Happy
|
6
|
+
# A collection of useful helper methods.
|
7
|
+
#
|
8
|
+
module Helpers
|
9
|
+
include Html
|
10
|
+
include Rendering
|
11
|
+
include I18n
|
12
|
+
|
13
|
+
# Some useful shortcuts.
|
14
|
+
#
|
15
|
+
alias_method :h, :escape_html
|
16
|
+
alias_method :l, :localize
|
17
|
+
alias_method :t, :translate
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Happy
|
2
|
+
class Request < Rack::Request
|
3
|
+
module DateParameterConverter
|
4
|
+
class << self
|
5
|
+
def convert!(params)
|
6
|
+
params.each do |k, v|
|
7
|
+
if looks_like_a_date?(v)
|
8
|
+
params[k] = convert_to_date(v)
|
9
|
+
elsif v.is_a? Hash
|
10
|
+
convert!(v)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def looks_like_a_date?(v)
|
16
|
+
v.is_a?(Hash) && v.has_key?('year') && v.has_key?('month') && v.has_key?('day')
|
17
|
+
end
|
18
|
+
|
19
|
+
def convert_to_date(v)
|
20
|
+
DateTime.new(v['year'].to_i, v['month'].to_i, v['day'].to_i, v['hour'].to_i, v['minute'].to_i, v['second'].to_i)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/happy/request.rb
CHANGED
@@ -1,17 +1,25 @@
|
|
1
|
-
|
2
|
-
require 'happy-helpers/utils/date_parameter_converter'
|
1
|
+
require 'happy/request/date_parameter_converter'
|
3
2
|
|
4
3
|
module Happy
|
5
4
|
# Happy's own little request class. It extends {Rack::Request} with
|
6
5
|
# a bit of convenience functionality.
|
7
6
|
#
|
8
7
|
class Request < Rack::Request
|
8
|
+
# Override the default #params method so it returns a Hash with indifferent
|
9
|
+
# access if ActiveSupport is available.
|
10
|
+
def params
|
11
|
+
@env['happy.params'] ||= if defined?(HashWithIndifferentAccess)
|
12
|
+
super.with_indifferent_access
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
9
17
|
|
10
18
|
protected
|
11
19
|
|
12
20
|
def parse_query(qs)
|
13
21
|
super(qs).tap do |p|
|
14
|
-
|
22
|
+
DateParameterConverter.convert!(p)
|
15
23
|
end
|
16
24
|
end
|
17
25
|
end
|
data/lib/happy/version.rb
CHANGED
data/lib/happy.rb
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
require 'rack'
|
2
|
+
|
3
|
+
# Happy is currently making use of ActiveSupport. I'm not 100% happy
|
4
|
+
# about this dependency and will eventually try to remove it.
|
5
|
+
# The following line should at least make sure that the individual
|
6
|
+
# components are autoloaded as needed.
|
7
|
+
require 'active_support'
|
8
|
+
|
2
9
|
require 'happy/version'
|
3
10
|
require 'happy/errors'
|
11
|
+
require 'happy/helpers'
|
4
12
|
require 'happy/controller'
|
5
13
|
|
6
14
|
module Happy
|
@@ -8,7 +8,7 @@ module Happy
|
|
8
8
|
|
9
9
|
describe '.set' do
|
10
10
|
it 'sets a class-level option' do
|
11
|
-
TestController.
|
11
|
+
TestController.settings[:foo].should == 'bar'
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -19,21 +19,21 @@ module Happy
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'sets an instance-level option, overriding the class default' do
|
22
|
-
@instance.
|
22
|
+
@instance.settings[:foo].should == 'baz'
|
23
23
|
end
|
24
24
|
|
25
25
|
it "doesn't modify the class-level default option" do
|
26
|
-
TestController.
|
26
|
+
TestController.settings[:foo].should == 'bar'
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
describe 'class-level
|
31
|
-
it 'are the defaults for instance-level
|
32
|
-
TestController.new.
|
30
|
+
describe 'class-level settings' do
|
31
|
+
it 'are the defaults for instance-level settings' do
|
32
|
+
TestController.new.settings[:foo].should == 'bar'
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
describe 'cascading
|
36
|
+
describe 'cascading settings' do
|
37
37
|
class OuterController < Controller
|
38
38
|
set :views, './foo/'
|
39
39
|
set :foo, 'bar'
|
@@ -44,15 +44,15 @@ module Happy
|
|
44
44
|
|
45
45
|
it "are copied from the parent controller if necessary" do
|
46
46
|
@instance = InnerController.new(OuterController.new)
|
47
|
-
@instance.
|
48
|
-
@instance.
|
47
|
+
@instance.settings[:views].should == './foo/'
|
48
|
+
@instance.settings[:foo].should be_nil
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
describe '
|
53
|
-
it "override default
|
52
|
+
describe 'settings passed to the initializer' do
|
53
|
+
it "override default settings" do
|
54
54
|
@instance = TestController.new({}, :foo => 'baz')
|
55
|
-
@instance.
|
55
|
+
@instance.settings[:foo].should == 'baz'
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module Happy
|
5
|
+
module Helpers
|
6
|
+
describe Html do
|
7
|
+
include Html
|
8
|
+
|
9
|
+
describe "#url_for" do
|
10
|
+
it "generates a url from an array of strings" do
|
11
|
+
url_for('foo', 'bar', '123').should == '/foo/bar/123'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "removes duplicate dashes" do
|
15
|
+
url_for('/foo/', 'bar').should == '/foo/bar'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "removes leading dashes" do
|
19
|
+
url_for('foo/').should == '/foo'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns just a slash if no parameters are given" do
|
23
|
+
url_for().should == '/'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "doesn't take nil into account" do
|
27
|
+
url_for('foo', nil, 'bar').should == '/foo/bar'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "also accepts symbols" do
|
31
|
+
url_for(:foo, 'bar').should == '/foo/bar'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "generates RESTful URLs from objects" do
|
35
|
+
url_for(mock(:class => 'thingy', :to_param => '1')).should == '/thingies/1'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "is can be cascaded" do
|
39
|
+
url_for(url_for(:foo, :bar), '123', url_for('woop')).should == '/foo/bar/123/woop'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "doesn't modify strings containing complete URLs" do
|
43
|
+
url_for('http://www.test.com').should == 'http://www.test.com'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Happy
|
4
|
+
describe Request do
|
5
|
+
describe '#params' do
|
6
|
+
subject do
|
7
|
+
Happy.route do
|
8
|
+
on('symbol') { "Your name is #{params[:name]}!" }
|
9
|
+
on('string') { "Your name is #{params['name']}!" }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "is accessible through strings" do
|
14
|
+
response_for { get '/string', 'name' => 'Hendrik' }.body.should == 'Your name is Hendrik!'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "is accessible through symbols" do
|
18
|
+
response_for { get '/symbol', 'name' => 'Hendrik' }.body.should == 'Your name is Hendrik!'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: happy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.pre27
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70179033071240 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70179033071240
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rack
|
27
|
-
requirement: &
|
27
|
+
requirement: &70179033070740 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,32 +32,32 @@ dependencies:
|
|
32
32
|
version: '1.4'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70179033070740
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement: &
|
37
|
+
name: allowance
|
38
|
+
requirement: &70179033070280 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
|
-
- -
|
41
|
+
- - ! '>='
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 0.1.
|
43
|
+
version: 0.1.1
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70179033070280
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement: &
|
48
|
+
name: tilt
|
49
|
+
requirement: &70179033069820 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: '1.3'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70179033069820
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &70179033085800 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70179033085800
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &70179033085260 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '2.8'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70179033085260
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec-html-matchers
|
82
|
-
requirement: &
|
82
|
+
requirement: &70179033084840 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70179033084840
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rack-test
|
93
|
-
requirement: &
|
93
|
+
requirement: &70179033084380 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70179033084380
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: watchr
|
104
|
-
requirement: &
|
104
|
+
requirement: &70179033083960 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,7 +109,7 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70179033083960
|
113
113
|
description: A happy little toolkit for writing web applications.
|
114
114
|
email:
|
115
115
|
- hendrik@mans.de
|
@@ -121,12 +121,15 @@ files:
|
|
121
121
|
- .rspec
|
122
122
|
- .travis.yml
|
123
123
|
- .watchr
|
124
|
-
- .
|
124
|
+
- .yardopts
|
125
125
|
- Gemfile
|
126
126
|
- LICENSE
|
127
127
|
- README.md
|
128
128
|
- Rakefile
|
129
|
+
- TUTORIAL.md
|
129
130
|
- example/config.ru
|
131
|
+
- example/views/block_helpers.erb
|
132
|
+
- example/views/block_helpers.haml
|
130
133
|
- example/views/index.erb
|
131
134
|
- example/views/layout.erb
|
132
135
|
- happy.gemspec
|
@@ -135,7 +138,6 @@ files:
|
|
135
138
|
- lib/happy/controller/actions.rb
|
136
139
|
- lib/happy/controller/cascadable.rb
|
137
140
|
- lib/happy/controller/configurable.rb
|
138
|
-
- lib/happy/controller/helpers.rb
|
139
141
|
- lib/happy/controller/permissions.rb
|
140
142
|
- lib/happy/controller/rackable.rb
|
141
143
|
- lib/happy/controller/routing.rb
|
@@ -147,7 +149,12 @@ files:
|
|
147
149
|
- lib/happy/extras/scriptable.rb
|
148
150
|
- lib/happy/extras/static.rb
|
149
151
|
- lib/happy/files/error.erb
|
152
|
+
- lib/happy/helpers.rb
|
153
|
+
- lib/happy/helpers/html.rb
|
154
|
+
- lib/happy/helpers/i18n.rb
|
155
|
+
- lib/happy/helpers/rendering.rb
|
150
156
|
- lib/happy/request.rb
|
157
|
+
- lib/happy/request/date_parameter_converter.rb
|
151
158
|
- lib/happy/response.rb
|
152
159
|
- lib/happy/version.rb
|
153
160
|
- spec/controller/actions_spec.rb
|
@@ -156,6 +163,8 @@ files:
|
|
156
163
|
- spec/controller/routing_spec.rb
|
157
164
|
- spec/controller_spec.rb
|
158
165
|
- spec/happy_spec.rb
|
166
|
+
- spec/helpers/html_spec.rb
|
167
|
+
- spec/request_spec.rb
|
159
168
|
- spec/spec_helper.rb
|
160
169
|
homepage: https://github.com/hmans/happy
|
161
170
|
licenses: []
|
@@ -188,5 +197,7 @@ test_files:
|
|
188
197
|
- spec/controller/routing_spec.rb
|
189
198
|
- spec/controller_spec.rb
|
190
199
|
- spec/happy_spec.rb
|
200
|
+
- spec/helpers/html_spec.rb
|
201
|
+
- spec/request_spec.rb
|
191
202
|
- spec/spec_helper.rb
|
192
203
|
has_rdoc:
|
data/.yardopt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--no-private --protected --embed-mixins lib/**/*.rb - README LEGAL
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'happy-helpers'
|
2
|
-
|
3
|
-
module Happy
|
4
|
-
class Controller
|
5
|
-
# A collection of useful helper methods.
|
6
|
-
#
|
7
|
-
module Helpers
|
8
|
-
# Load a whole bunch of helpers fromi HappyHelpers. This includes stuff
|
9
|
-
# like url_for, link_to and more.
|
10
|
-
include HappyHelpers::Helpers
|
11
|
-
|
12
|
-
# Renders "something". This method takes a closer look at what this
|
13
|
-
# "something" is and then dispatches to a more specific method.
|
14
|
-
#
|
15
|
-
def render(what, options = {}, &blk)
|
16
|
-
case what
|
17
|
-
when NilClass then ''
|
18
|
-
when String then render_template(what, options, &blk)
|
19
|
-
when Enumerable then what.map { |i| render(i, options, &blk) }.join
|
20
|
-
else render_resource(what, options)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
# Render a template from the controller's view folder.
|
25
|
-
#
|
26
|
-
def render_template(name, variables = {}, &blk)
|
27
|
-
path = options[:views] || './views'
|
28
|
-
HappyHelpers::Templates.render(File.join(path, name), self, variables, &blk)
|
29
|
-
end
|
30
|
-
|
31
|
-
# Render a resource.
|
32
|
-
#
|
33
|
-
def render_resource(resource, options = {})
|
34
|
-
# build name strings
|
35
|
-
singular_name = resource.class.to_s.tableize.singularize
|
36
|
-
plural_name = singular_name.pluralize
|
37
|
-
|
38
|
-
# set options
|
39
|
-
options = {
|
40
|
-
singular_name => resource
|
41
|
-
}.merge(options)
|
42
|
-
|
43
|
-
# render
|
44
|
-
render_template("#{plural_name}/_#{singular_name}.html.haml", options)
|
45
|
-
end
|
46
|
-
|
47
|
-
alias_method :h, :escape_html
|
48
|
-
alias_method :l, :localize
|
49
|
-
alias_method :t, :translate
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|