sinatra_more 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Nathan Esquenazi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,16 @@
1
+ = sinatra_more
2
+
3
+ This will be a plugin which expands sinatra in a variety of ways. Not ready to be used yet!
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ * Send me a pull request. Bonus points for topic branches.
13
+
14
+ == Copyright
15
+
16
+ Copyright (c) 2009 Nathan Esquenazi. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ # $ rake version:bump:patch release
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "sinatra_more"
10
+ gem.summary = "Expands sinatra to allow for complex applications"
11
+ gem.description = "Expands sinatra with standard helpers and tools to allow for complex applications"
12
+ gem.email = "nesquena@gmail.com"
13
+ gem.homepage = "http://github.com/nesquena/sinatra_more"
14
+ gem.authors = ["Nathan Esquenazi"]
15
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "sinatra_more #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,60 @@
1
+ class AbstractFormBuilder
2
+ attr_accessor :template, :object
3
+
4
+ def initialize(template, object)
5
+ raise "FormBuilder template must be initialized!" unless template
6
+ raise "FormBuilder object must be initialized!" unless object
7
+ @template = template
8
+ @object = object
9
+ end
10
+
11
+ def error_messages(options={})
12
+ @template.error_messages_for(@object, options)
13
+ end
14
+
15
+ def label(field, options={})
16
+ options.reverse_merge!(:caption => field.to_s.titleize)
17
+ @template.label_tag(field_id(field), options)
18
+ end
19
+
20
+ def text_field(field, options={})
21
+ options.reverse_merge!(:value => field_value(field))
22
+ @template.text_field_tag field_name(field), options
23
+ end
24
+
25
+ def text_area(field, options={})
26
+ options.reverse_merge!(:value => field_value(field))
27
+ @template.text_area_tag field_name(field), options
28
+ end
29
+
30
+ def password_field(field, options={})
31
+ options.reverse_merge!(:value => field_value(field))
32
+ @template.password_field_tag field_name(field), options
33
+ end
34
+
35
+ def file_field(field, options={})
36
+ @template.file_field_tag field_name(field), options
37
+ end
38
+
39
+ def submit(caption, options={})
40
+ @template.submit_tag caption, options
41
+ end
42
+
43
+ private
44
+
45
+ def object_name
46
+ object.class.to_s.underscore
47
+ end
48
+
49
+ def field_value(field)
50
+ @object && @object.respond_to?(field) ? @object.send(field) : ""
51
+ end
52
+
53
+ def field_name(field)
54
+ "#{object_name}[#{field}]"
55
+ end
56
+
57
+ def field_id(field)
58
+ "#{object_name}_#{field}"
59
+ end
60
+ end
@@ -0,0 +1,35 @@
1
+ class StandardFormBuilder < AbstractFormBuilder
2
+ def text_field_block(field, options={})
3
+ @template.content_block_tag(:p) do
4
+ html = label(field)
5
+ html << text_field(field, options)
6
+ end
7
+ end
8
+
9
+ def text_area_block(field, options={})
10
+ @template.content_block_tag(:p) do
11
+ html = label(field)
12
+ html << text_area(field, options)
13
+ end
14
+ end
15
+
16
+ def password_field_block(field, options={})
17
+ @template.content_block_tag(:p) do
18
+ html = label(field)
19
+ html << password_field(field, options)
20
+ end
21
+ end
22
+
23
+ def file_field_block(field, options={})
24
+ @template.content_block_tag(:p) do
25
+ html = label(field)
26
+ html << file_field(field, options)
27
+ end
28
+ end
29
+
30
+ def submit_block(caption)
31
+ @template.content_block_tag(:p) do
32
+ @template.submit_tag(caption)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ module SinatraMore
2
+ module AssetTagHelpers
3
+ def link_to(name, url='javascript:void(0)', options={})
4
+ options.reverse_merge!(:href => url)
5
+ content_tag(:a, name, options)
6
+ end
7
+
8
+ def image_tag(url, options={})
9
+ options.reverse_merge!(:src => url)
10
+ tag(:img, options)
11
+ end
12
+
13
+ def stylesheet_link_tag(*sources)
14
+ options = sources.extract_options!.symbolize_keys
15
+ sources.collect { |sheet| stylesheet_tag(sheet, options) }.join("\n")
16
+ end
17
+
18
+ def stylesheet_tag(source, options={})
19
+ rel_path = "/stylesheets/#{source}.css?#{Time.now.to_i}"
20
+ options = options.dup.reverse_merge!(:href => rel_path, :media => 'screen', :rel => 'stylesheet', :type => 'text/css')
21
+ tag(:link, options)
22
+ end
23
+
24
+ def javascript_include_tag(*sources)
25
+ options = sources.extract_options!.symbolize_keys
26
+ sources.collect { |script| javascript_tag(script, options) }.join("\n")
27
+ end
28
+
29
+ def javascript_tag(source, options={})
30
+ rel_path = "/javascripts/#{source}.js?#{Time.now.to_i}"
31
+ options = options.dup.reverse_merge!(:content => "", :src => rel_path, :type => 'text/javascript')
32
+ tag(:script, options)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,69 @@
1
+ module SinatraMore
2
+ module FormHelpers
3
+ # form_for @user, '/register', :id => 'register'
4
+ def form_for(object, url, settings={}, &block)
5
+ default_builder = settings[:builder] || self.options.default_builder.constantize
6
+ settings.reverse_merge!(:method => 'post', :action => url)
7
+ settings[:enctype] = "multipart/form-data" if settings.delete(:multipart)
8
+ # TODO make this work with erb!!
9
+ form_html = capture_haml(default_builder.new(self, object), &block)
10
+ haml_concat content_tag('form', form_html, settings)
11
+ end
12
+
13
+ # form_tag '/register' do ... end
14
+ def form_tag(url, options={}, &block)
15
+ options.reverse_merge!(:method => 'post', :action => url)
16
+ # TODO make this work with erb!!
17
+ haml_concat content_tag('form', capture_haml(&block), options)
18
+ end
19
+
20
+ # error_messages_for @user
21
+ def error_messages_for(record, options={})
22
+ return "" if record.blank? or record.errors.none?
23
+ options.reverse_merge!(:header_message => "The #{record.class.to_s.downcase} could not be saved!")
24
+ error_messages = record.errors.full_messages
25
+ content_block_tag(:div, :class => 'field-errors') do
26
+ html = content_tag(:p, options.delete(:header_message))
27
+ html << content_block_tag(:ul, :class => 'field-errors') do
28
+ error_messages.collect { |er| content_tag(:li, er) }.join("\n")
29
+ end
30
+ end
31
+ end
32
+
33
+ # label_tag :username
34
+ def label_tag(name, options={})
35
+ options.reverse_merge!(:caption => name.to_s.titleize)
36
+ content_tag(:label, options.delete(:caption) + ": ", :for => name)
37
+ end
38
+
39
+ # text_field_tag :username
40
+ def text_field_tag(name, options={})
41
+ options.reverse_merge!(:name => name)
42
+ input_tag(:text, options)
43
+ end
44
+
45
+ # text_field_tag :username
46
+ def text_area_tag(name, options={})
47
+ options.reverse_merge!(:name => name)
48
+ content_tag(:textarea, '', options)
49
+ end
50
+
51
+ # password_field_tag :password
52
+ def password_field_tag(name, options={})
53
+ options.reverse_merge!(:name => name)
54
+ input_tag(:password, options)
55
+ end
56
+
57
+ # field_field_tag
58
+ def file_field_tag(name, options={})
59
+ options.reverse_merge!(:name => name)
60
+ input_tag(:file, options)
61
+ end
62
+
63
+ # submit_tag "Create"
64
+ def submit_tag(caption, options={})
65
+ options.reverse_merge!(:value => caption)
66
+ input_tag(:submit, options)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,30 @@
1
+ module SinatraMore
2
+ module FormatHelpers
3
+ def relative_time_ago(date)
4
+ date = date.to_date
5
+ date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
6
+ days = (date - Date.today).to_i
7
+
8
+ return 'today' if days >= 0 and days < 1
9
+ return 'tomorrow' if days >= 1 and days < 2
10
+ return 'yesterday' if days >= -1 and days < 0
11
+
12
+ return "in #{days} days" if days.abs < 60 and days > 0
13
+ return "#{days.abs} days ago" if days.abs < 60 and days < 0
14
+
15
+ return date.strftime('%A, %B %e') if days.abs < 182
16
+ return date.strftime('%A, %B %e, %Y')
17
+ end
18
+
19
+ def escape_javascript(javascript)
20
+ return '' unless javascript
21
+ javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n' }
22
+ javascript_mapping.merge("\r" => '\n', '"' => '\\"', "'" => "\\'")
23
+ escaped_string = javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
24
+ "\"#{escaped_string}\""
25
+ end
26
+
27
+ alias js_escape escape_javascript
28
+
29
+ end
30
+ end
@@ -0,0 +1,54 @@
1
+ module SinatraMore
2
+ module RenderHelpers
3
+ # Renders a erb template based on the relative path
4
+ # erb_template 'users/new'
5
+ def erb_template(template_path, options={})
6
+ render_template template_path, options.merge(:template_engine => :erb)
7
+ end
8
+
9
+ # Renders a haml template based on the relative path
10
+ # haml_template 'users/new'
11
+ def haml_template(template_path, options={})
12
+ render_template template_path, options.merge(:template_engine => :haml)
13
+ end
14
+
15
+ # Renders a template from a file path automatically determining rendering engine
16
+ # render_template 'users/new'
17
+ # options = { :template_engine => 'haml' }
18
+ def render_template(template_path, options={})
19
+ template_engine = options.delete(:template_engine) || resolve_template_engine(template_path)
20
+ render template_engine.to_sym, template_path.to_sym, options
21
+ end
22
+
23
+ # Partials implementation which includes collections support
24
+ # partial 'photo/_item', :object => @photo
25
+ # partial 'photo/_item', :collection => @photos
26
+ def partial(template, *args)
27
+ options = args.extract_options!
28
+ options.merge!(:layout => false)
29
+ path = template.to_s.split(File::SEPARATOR)
30
+ object = path[-1].to_sym
31
+ path[-1] = "_#{path[-1]}"
32
+ template_path = File.join(path)
33
+ if collection = options.delete(:collection)
34
+ collection.inject([]) do |buffer, member|
35
+ collection_options = options.merge(:layout => false, :locals => {object => member})
36
+ buffer << render_template(template_path, collection_options)
37
+ end.join("\n")
38
+ else
39
+ render_template(template_path, options)
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ # Returns the template engine (i.e haml) to use for a given template_path
46
+ # resolve_template_engine('users/new') => :haml
47
+ def resolve_template_engine(template_path)
48
+ resolved_template_path = File.join(self.options.views, template_path + ".*")
49
+ template_file = Dir.glob(resolved_template_path).first
50
+ raise "Template path '#{template_path}' could not be located in views!" unless template_file
51
+ template_engine = File.extname(template_file)[1..-1].to_sym
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ module SinatraMore
2
+ module TagHelpers
3
+ # input_tag :text, :class => "test"
4
+ def input_tag(type, options = {})
5
+ options.reverse_merge!(:type => type)
6
+ tag(:input, options)
7
+ end
8
+
9
+ def content_block_tag(name, options={}, &block)
10
+ # TODO make this work with erb!!
11
+ options.merge!(:content => block.call)
12
+ tag(name, options)
13
+ end
14
+
15
+ def content_tag(name, content, options={})
16
+ tag(name, options.merge(:content => content))
17
+ end
18
+
19
+ def tag(name, options={})
20
+ content = options.delete(:content)
21
+ html_attrs = options.collect { |a, v| v.blank? ? nil : "#{a}=\"#{v}\"" }.compact.join(" ")
22
+ base_tag = (html_attrs.present? ? "<#{name} #{html_attrs}" : "<#{name}")
23
+ base_tag << (content ? ">#{content}</#{name}>" : " />")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1 @@
1
+ Dir.glob(File.dirname(__FILE__) + '/view_helpers/*.rb').each { |f| require f }
@@ -0,0 +1,14 @@
1
+ require 'sinatra/base'
2
+
3
+ require File.join(File.dirname(__FILE__) + '/sinatra_more/view_helpers')
4
+ Dir.glob(File.dirname(__FILE__) + '/sinatra_more/form_builder/*.rb').each { |f| require f }
5
+
6
+ module SinatraMore
7
+ def self.registered(app)
8
+ app.helpers TagHelpers
9
+ app.helpers AssetTagHelpers
10
+ app.helpers FormHelpers
11
+ app.helpers FormatHelpers
12
+ app.helpers RenderHelpers
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sinatra_more}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nathan Esquenazi"]
12
+ s.date = %q{2009-10-24}
13
+ s.description = %q{Expands sinatra with standard helpers and tools to allow for complex applications}
14
+ s.email = %q{nesquena@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/sinatra_more.rb",
27
+ "lib/sinatra_more/form_builder/abstract_form_builder.rb",
28
+ "lib/sinatra_more/form_builder/standard_form_builder.rb",
29
+ "lib/sinatra_more/view_helpers.rb",
30
+ "lib/sinatra_more/view_helpers/asset_tag_helpers.rb",
31
+ "lib/sinatra_more/view_helpers/form_helpers.rb",
32
+ "lib/sinatra_more/view_helpers/format_helpers.rb",
33
+ "lib/sinatra_more/view_helpers/render_helpers.rb",
34
+ "lib/sinatra_more/view_helpers/tag_helpers.rb",
35
+ "sinatra_more.gemspec",
36
+ "test/helper.rb",
37
+ "test/test_sinatra_more.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/nesquena/sinatra_more}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.5}
43
+ s.summary = %q{Expands sinatra to allow for complex applications}
44
+ s.test_files = [
45
+ "test/helper.rb",
46
+ "test/test_sinatra_more.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ end
61
+ end
62
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'sinatra_more'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestSinatraMore < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra_more
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Esquenazi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Expands sinatra with standard helpers and tools to allow for complex applications
26
+ email: nesquena@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/sinatra_more.rb
42
+ - lib/sinatra_more/form_builder/abstract_form_builder.rb
43
+ - lib/sinatra_more/form_builder/standard_form_builder.rb
44
+ - lib/sinatra_more/view_helpers.rb
45
+ - lib/sinatra_more/view_helpers/asset_tag_helpers.rb
46
+ - lib/sinatra_more/view_helpers/form_helpers.rb
47
+ - lib/sinatra_more/view_helpers/format_helpers.rb
48
+ - lib/sinatra_more/view_helpers/render_helpers.rb
49
+ - lib/sinatra_more/view_helpers/tag_helpers.rb
50
+ - sinatra_more.gemspec
51
+ - test/helper.rb
52
+ - test/test_sinatra_more.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/nesquena/sinatra_more
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Expands sinatra to allow for complex applications
81
+ test_files:
82
+ - test/helper.rb
83
+ - test/test_sinatra_more.rb