happy-helpers 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/happy-helpers.rb +6 -0
- data/lib/happy-helpers/helpers.rb +11 -0
- data/lib/happy-helpers/helpers/forms.rb +169 -0
- data/lib/happy-helpers/helpers/html.rb +49 -0
- data/lib/happy-helpers/helpers/i18n.rb +13 -0
- data/lib/happy-helpers/templates.rb +18 -0
- data/lib/happy-helpers/utils/date_parameter_converter.rb +25 -0
- data/lib/happy-helpers/version.rb +3 -0
- data/niles.gemspec +19 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Hendrik Mans
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# HappyHelpers
|
2
|
+
|
3
|
+
A set of mixins providing useful view helpers for custom Rack applications or frameworks.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'happy-helpers'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install happy-helpers
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
module HappyHelpers
|
2
|
+
module Helpers
|
3
|
+
module Forms
|
4
|
+
def radio_buttons(name, options = {})
|
5
|
+
"".tap do |s|
|
6
|
+
options[:options].each do |option|
|
7
|
+
if option.is_a?(Array)
|
8
|
+
label = option[0]
|
9
|
+
value = option[1]
|
10
|
+
else
|
11
|
+
label = option
|
12
|
+
value = option
|
13
|
+
end
|
14
|
+
|
15
|
+
s << html_tag(:label) do
|
16
|
+
html_tag(:input, type: 'radio', name: name, value: value) + label
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def date_time_select_tag(name, value = nil, options = {})
|
23
|
+
options = {
|
24
|
+
:years => (Time.now.year - 10)..(Time.now.year + 10),
|
25
|
+
:months => 1..12,
|
26
|
+
:days => 1..31,
|
27
|
+
:month_formatter => ->(i) { translate("date.month_names")[i] },
|
28
|
+
:day_formatter => ->(i) { "#{i}." }
|
29
|
+
}.merge(options)
|
30
|
+
value ||= Time.now
|
31
|
+
|
32
|
+
year_select = html_tag(:select, name: "#{name}[year]") do
|
33
|
+
options[:years].map do |year|
|
34
|
+
html_tag(:option, value: year, selected: year == value.try(:year)) { year }
|
35
|
+
end.join
|
36
|
+
end
|
37
|
+
|
38
|
+
month_select = html_tag(:select, name: "#{name}[month]") do
|
39
|
+
options[:months].map do |month|
|
40
|
+
html_tag(:option, value: month, selected: month == value.try(:month)) { options[:month_formatter].call(month) }
|
41
|
+
end.join
|
42
|
+
end
|
43
|
+
|
44
|
+
day_select = html_tag(:select, name: "#{name}[day]") do
|
45
|
+
options[:days].map do |day|
|
46
|
+
html_tag(:option, value: day, selected: day == value.try(:day)) { options[:day_formatter].call(day) }
|
47
|
+
end.join
|
48
|
+
end
|
49
|
+
|
50
|
+
hour_select = html_tag(:select, name: "#{name}[hour]") do
|
51
|
+
(0..23).map do |hour|
|
52
|
+
html_tag(:option, value: hour, selected: hour == value.try(:hour)) { sprintf "%02d", hour }
|
53
|
+
end.join
|
54
|
+
end
|
55
|
+
|
56
|
+
minute_select = html_tag(:select, name: "#{name}[minute]") do
|
57
|
+
(0..59).map do |minute|
|
58
|
+
html_tag(:option, value: minute, selected: minute == value.try(:min)) { sprintf "%02d", minute }
|
59
|
+
end.join
|
60
|
+
end
|
61
|
+
|
62
|
+
"#{day_select} #{month_select} #{year_select} - #{hour_select} : #{minute_select} Uhr"
|
63
|
+
end
|
64
|
+
|
65
|
+
def checkbox_tag(name, value = false, options = {})
|
66
|
+
"".tap do |s|
|
67
|
+
s << html_tag(:input, name: name, type: 'hidden', value: '0')
|
68
|
+
s << html_tag(:input, name: name, type: 'checkbox', value: '1', checked: value ? true : false)
|
69
|
+
s << html_tag(:label, class: 'for-checkbox') { options[:text] || name }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def form_for(resource, options = {}, &block)
|
74
|
+
options = {
|
75
|
+
action: resource.new_record? ? "/#{resource.class.to_s.tableize}" : "/#{resource.class.to_s.tableize}/#{resource.id}",
|
76
|
+
method: 'post'
|
77
|
+
}.merge(options)
|
78
|
+
|
79
|
+
html_tag :form, options do
|
80
|
+
capture do
|
81
|
+
yield FormBuilder.new(self, resource, options)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class FormBuilder
|
87
|
+
attr_reader :helpers, :resource, :form_options
|
88
|
+
|
89
|
+
def initialize(helpers, resource, form_options = {})
|
90
|
+
@helpers = helpers
|
91
|
+
@resource = resource
|
92
|
+
@resource_name = resource.class.to_s.singularize.underscore
|
93
|
+
@form_options = form_options
|
94
|
+
end
|
95
|
+
|
96
|
+
def input(name, options = {})
|
97
|
+
name = name.to_s
|
98
|
+
value = resource.send(name)
|
99
|
+
|
100
|
+
# Set default options
|
101
|
+
options = {
|
102
|
+
label: "%s:" % label_text(name)
|
103
|
+
}.merge(options)
|
104
|
+
|
105
|
+
options[:as] ||= case options[:value]
|
106
|
+
when Date then :date
|
107
|
+
when DateTime, Time then :datetime
|
108
|
+
when Boolean, TrueClass, FalseClass then :checkbox
|
109
|
+
else :text
|
110
|
+
end
|
111
|
+
|
112
|
+
# Set default options for the input field
|
113
|
+
field_options = {
|
114
|
+
name: "%s[%s]" % [@resource_name, name]
|
115
|
+
}
|
116
|
+
unless (placeholder = options.delete(:placeholder) || placeholder(name)).blank?
|
117
|
+
field_options[:placeholder] = placeholder
|
118
|
+
end
|
119
|
+
|
120
|
+
# Set default options for the wrapper element
|
121
|
+
wrapper_options = {
|
122
|
+
class: "input #{options[:as]}"
|
123
|
+
}
|
124
|
+
wrapper_options[:class] << " with_error" if resource.errors[name].any?
|
125
|
+
|
126
|
+
# Output wrapper tag with contents
|
127
|
+
helpers.html_tag :div, wrapper_options do
|
128
|
+
String.new.tap do |s|
|
129
|
+
# Add actual input field
|
130
|
+
case options.delete(:as).to_sym
|
131
|
+
when :textarea
|
132
|
+
s << helpers.html_tag(:label, class: 'for-field') { options[:label] }
|
133
|
+
s << helpers.html_tag(:textarea, field_options) { helpers.preserve(helpers.escape_html(value)) }
|
134
|
+
when :datetime
|
135
|
+
s << helpers.html_tag(:label, class: 'for-field') { options[:label] }
|
136
|
+
s << helpers.date_time_select_tag(field_options.delete(:name), value, options)
|
137
|
+
when :radio
|
138
|
+
s << helpers.radio_buttons(field_options.delete(:name), options: (options.delete(:options) || ['Y', 'N']))
|
139
|
+
when :checkbox
|
140
|
+
s << helpers.checkbox_tag(field_options.delete(:name), value, options.merge(:text => options[:text] || label_text(name)))
|
141
|
+
else
|
142
|
+
s << helpers.html_tag(:label, class: 'for-field') { options[:label] }
|
143
|
+
s << helpers.html_tag(:input, field_options.merge(type: 'text', value: value))
|
144
|
+
end
|
145
|
+
|
146
|
+
# Add error message, if necessary
|
147
|
+
if @resource.errors[name].any?
|
148
|
+
s << helpers.html_tag(:div, :class => "error") { @resource.errors[name].first }
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def label_text(name)
|
155
|
+
helpers.translate("forms.#{@resource_name}.labels.#{name}", :default => name.humanize)
|
156
|
+
end
|
157
|
+
|
158
|
+
def placeholder(name)
|
159
|
+
helpers.translate("forms.#{@resource_name}.placeholders.#{name}", :default => "")
|
160
|
+
end
|
161
|
+
|
162
|
+
def submit(label = nil)
|
163
|
+
label ||= (resource.new_record? ? 'Create' : 'Update')
|
164
|
+
helpers.html_tag(:input, type: 'submit', value: label)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module HappyHelpers
|
2
|
+
module Helpers
|
3
|
+
module Html
|
4
|
+
def html_tag(name, options = nil, escape = true, &block)
|
5
|
+
"<#{name} #{html_tag_attributes(options, escape) if options}#{block_given? ? ">#{yield if block_given?}</#{name}>" : " />"}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def html_tag_attributes(options, escape = true)
|
9
|
+
options.map do |k,v|
|
10
|
+
if v
|
11
|
+
v == true ? "#{k}" : "#{k}=\"#{ escape_html(v) }\""
|
12
|
+
end
|
13
|
+
end.compact.join(" ")
|
14
|
+
end
|
15
|
+
|
16
|
+
def escape_html(t)
|
17
|
+
#Rack::Utils.escape_html(t.to_s)
|
18
|
+
CGI::escape_html(t.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
def preserve(t)
|
22
|
+
t.chomp("\n").gsub(/\n/, '
').gsub(/\r/, '')
|
23
|
+
end
|
24
|
+
|
25
|
+
def link_to(name, *target)
|
26
|
+
options = target.last.is_a?(Hash) ? target.pop : {}
|
27
|
+
html_tag(:a, options.merge(href: url_for(*target))) { name }
|
28
|
+
end
|
29
|
+
|
30
|
+
def url_for(*what)
|
31
|
+
return what.first if what.size == 1 && what.first.is_a?(String)
|
32
|
+
|
33
|
+
what.flatten.inject('') do |url, item|
|
34
|
+
url << "/%s" % case item
|
35
|
+
when String, Symbol then item.to_s
|
36
|
+
else "%s/%s" % [item.class.to_s.tableize.pluralize, item.try(:to_param) || item.try(:to_id) || item.try(:id)]
|
37
|
+
end
|
38
|
+
|
39
|
+
url
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def capture(*args, &block)
|
44
|
+
# TODO: support more than just HAML. :P~
|
45
|
+
capture_haml(*args, &block)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
|
3
|
+
module HappyHelpers
|
4
|
+
module Templates
|
5
|
+
def self.render(name, scope = nil, variables = {}, &block)
|
6
|
+
load("views/%s" % name).render(scope, variables, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.load(name)
|
10
|
+
if false # Freddie.env.production?
|
11
|
+
@templates ||= {}
|
12
|
+
@templates[name] ||= Tilt.new(name, default_encoding: 'utf-8')
|
13
|
+
else
|
14
|
+
Tilt.new(name, default_encoding: 'utf-8')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module HappyHelpers
|
2
|
+
module Utils
|
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/niles.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/happy-helpers/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Hendrik Mans"]
|
6
|
+
gem.email = ["hendrik@mans.de"]
|
7
|
+
gem.description = %q{View helpers for your custom Rack app or framework.}
|
8
|
+
gem.summary = %q{View helpers for your custom Rack app or framework.}
|
9
|
+
gem.homepage = "https://github.com/hmans/happy-helpers"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "happy-helpers"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = HappyHelpers::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'tilt', '~> 1.3'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: happy-helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hendrik Mans
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: tilt
|
16
|
+
requirement: &70215750342640 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70215750342640
|
25
|
+
description: View helpers for your custom Rack app or framework.
|
26
|
+
email:
|
27
|
+
- hendrik@mans.de
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/happy-helpers.rb
|
38
|
+
- lib/happy-helpers/helpers.rb
|
39
|
+
- lib/happy-helpers/helpers/forms.rb
|
40
|
+
- lib/happy-helpers/helpers/html.rb
|
41
|
+
- lib/happy-helpers/helpers/i18n.rb
|
42
|
+
- lib/happy-helpers/templates.rb
|
43
|
+
- lib/happy-helpers/utils/date_parameter_converter.rb
|
44
|
+
- lib/happy-helpers/version.rb
|
45
|
+
- niles.gemspec
|
46
|
+
homepage: https://github.com/hmans/happy-helpers
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.11
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: View helpers for your custom Rack app or framework.
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|