niles 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in niles.gemspec
4
+ gemspec
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
+ # Niles
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 'niles'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install niles
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,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/niles.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "niles/version"
2
+ require 'niles/helpers'
3
+ require 'niles/templates'
4
+
5
+ module Niles
6
+ end
@@ -0,0 +1,81 @@
1
+ # currently untested, probably not working!
2
+ #
3
+ module Niles
4
+ class FormBuilder
5
+ attr_reader :helpers, :resource, :form_options
6
+
7
+ def initialize(helpers, resource, form_options = {})
8
+ @helpers = helpers
9
+ @resource = resource
10
+ @resource_name = resource.class.to_s.singularize.underscore
11
+ @form_options = form_options
12
+ end
13
+
14
+ def input(name, options = {})
15
+ name = name.to_s
16
+
17
+ # Set default options
18
+ options = {
19
+ label: "%s:" % label_text(name),
20
+ value: resource.send(name)
21
+ }.merge(options)
22
+
23
+ # FIXME
24
+ options[:as] ||= case options[:value]
25
+ when Date then :date
26
+ when DateTime, Time then :datetime
27
+ else :text
28
+ end
29
+
30
+ # Set default options for the input field
31
+ field_options = {
32
+ name: "%s[%s]" % [@resource_name, name]
33
+ }
34
+ unless (placeholder = options.delete(:placeholder) || placeholder(name)).blank?
35
+ field_options[:placeholder] = placeholder
36
+ end
37
+
38
+ # Set default options for the wrapper element
39
+ wrapper_options = {
40
+ class: "input #{options[:as]}"
41
+ }
42
+ wrapper_options[:class] << " with_error" if resource.errors[name].any?
43
+
44
+ # Output wrapper tag with contents
45
+ helpers.html_tag :div, wrapper_options do
46
+ String.new.tap do |s|
47
+ # Add label
48
+ s << helpers.html_tag(:label) { options[:label] }
49
+
50
+ # Add actual input field
51
+ case options[:as].to_sym
52
+ when :textarea
53
+ s << helpers.html_tag(:textarea, field_options) { helpers.preserve(helpers.escape_html(options[:value])) }
54
+ when :datetime
55
+ s << helpers.date_time_select(field_options.delete(:name), value: options[:value])
56
+ else
57
+ s << helpers.html_tag(:input, field_options.merge(type: 'text', value: options[:value]))
58
+ end
59
+
60
+ # Add error message, if necessary
61
+ if @resource.errors[name].any?
62
+ s << helpers.html_tag(:div, :class => "error") { @resource.errors[name].first }
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ def label_text(name)
69
+ helpers.translate("forms.#{@resource_name}.labels.#{name}", :default => name.humanize)
70
+ end
71
+
72
+ def placeholder(name)
73
+ helpers.translate("forms.#{@resource_name}.placeholders.#{name}", :default => "")
74
+ end
75
+
76
+ def submit(label = nil)
77
+ label ||= (resource.new_record? ? 'Create' : 'Update')
78
+ helpers.html_tag(:input, type: 'submit', value: label)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,11 @@
1
+ require 'niles/helpers/html'
2
+ require 'niles/helpers/forms'
3
+ require 'niles/helpers/i18n'
4
+
5
+ module Niles
6
+ module Helpers
7
+ extend Html
8
+ extend I18n
9
+ extend Forms
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ module Niles
2
+ module Helpers
3
+ module Forms
4
+ def date_time_select(name, options = {})
5
+ options = {
6
+ :years => 1976..2012,
7
+ :months => 1..12,
8
+ :days => 1..31,
9
+ :month_formatter => ->(i) { translate("date.month_names")[i] },
10
+ :day_formatter => ->(i) { "#{i}." },
11
+ :value => Time.now
12
+ }.merge(options)
13
+
14
+ year_select = html_tag(:select, name: "#{name}[year]") do
15
+ options[:years].map do |year|
16
+ html_tag(:option, value: year, selected: year == options[:value].try(:year)) { year }
17
+ end.join
18
+ end
19
+
20
+ month_select = html_tag(:select, name: "#{name}[month]") do
21
+ options[:months].map do |month|
22
+ html_tag(:option, value: month, selected: month == options[:value].try(:month)) { options[:month_formatter].call(month) }
23
+ end.join
24
+ end
25
+
26
+ day_select = html_tag(:select, name: "#{name}[day]") do
27
+ options[:days].map do |day|
28
+ html_tag(:option, value: day, selected: day == options[:value].try(:day)) { options[:day_formatter].call(day) }
29
+ end.join
30
+ end
31
+
32
+ hour_select = html_tag(:select, name: "#{name}[hour]") do
33
+ (0..23).map do |hour|
34
+ html_tag(:option, value: hour, selected: hour == options[:value].try(:hour)) { sprintf "%02d", hour }
35
+ end.join
36
+ end
37
+
38
+ minute_select = html_tag(:select, name: "#{name}[minute]") do
39
+ (0..59).map do |minute|
40
+ html_tag(:option, value: minute, selected: minute == options[:value].try(:minute)) { sprintf "%02d", minute }
41
+ end.join
42
+ end
43
+
44
+ "#{day_select} #{month_select} #{year_select} - #{hour_select} : #{minute_select} Uhr"
45
+ end
46
+
47
+ def form_for(resource, options = {}, &block)
48
+ options = {
49
+ action: resource.new_record? ? "/#{resource.class.to_s.tableize}" : "/#{resource.class.to_s.tableize}/#{resource.id}",
50
+ method: 'post'
51
+ }.merge(options)
52
+
53
+ html_tag :form, options do
54
+ capture do
55
+ yield FormBuilder.new(self, resource, options)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ module Niles
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
+ end
19
+
20
+ def preserve(t)
21
+ t.chomp("\n").gsub(/\n/, '&#x000A;').gsub(/\r/, '')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Niles
2
+ module Helpers
3
+ module I18n
4
+ def translate(*args)
5
+ I18n.translate(*args)
6
+ end
7
+
8
+ def localize(*args)
9
+ I18n.localize(*args)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require 'tilt'
2
+
3
+ module Niles
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,3 @@
1
+ module Niles
2
+ VERSION = "0.0.1"
3
+ end
data/niles.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/niles/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/niles"
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 = "niles"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Niles::VERSION
17
+
18
+ gem.add_dependency 'tilt', '~> 1.3'
19
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: niles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hendrik Mans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: tilt
16
+ requirement: &70331604541560 !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: *70331604541560
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/niles.rb
38
+ - lib/niles/form_builder.rb
39
+ - lib/niles/helpers.rb
40
+ - lib/niles/helpers/forms.rb
41
+ - lib/niles/helpers/html.rb
42
+ - lib/niles/helpers/i18n.rb
43
+ - lib/niles/templates.rb
44
+ - lib/niles/version.rb
45
+ - niles.gemspec
46
+ homepage: https://github.com/hmans/niles
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: