simple_show 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use simple_show
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :test do
4
+ gem 'rake', '0.8.7'
5
+ gem 'rails'
6
+ gem 'sqlite3'
7
+ gem 'nokogiri'
8
+ end
9
+
10
+ # Specify your gem's dependencies in simple_show.gemspec
11
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,95 @@
1
+ == simple_show
2
+
3
+ * http://github.com/phallstrom/simple_show
4
+
5
+ == Description:
6
+
7
+ SimpleShow is to #show as SimpleForm is to #new/#edit.
8
+
9
+ == Requirements:
10
+
11
+ * Rails 3
12
+
13
+ == Install:
14
+
15
+ Add it to your Gemfile:
16
+
17
+ gem "simple_show"
18
+
19
+ Update your bundle:
20
+
21
+ bundle update simple_show
22
+
23
+ Run the generator:
24
+
25
+ rails generate simple_show:install
26
+
27
+ == Example Usage:
28
+
29
+ Assume we have a Person object with name, phone, email, born_on, etc.
30
+ In our show action we can then do this:
31
+
32
+ <%= simple_show_for @johndoe do |f| %>
33
+ <%= f.show :name %>
34
+ <%= f.show :phone, :label => 'Telephone' %>
35
+ <%= f.show(:email) {|o| o.email.downcase} %>
36
+ <%= f.show :born_on, :value => '**censored**' %>
37
+ <% end %>
38
+
39
+ Which would generate (using the defaults) output like this:
40
+
41
+ <div class="simple_show person" id="person_123">
42
+ <div class="show">
43
+ <label>Name:</label>
44
+ <span class="value">John Doe</span>
45
+ </div>
46
+ <div class="show">
47
+ <label>Telephone:</label>
48
+ <span class="value">867-5309</span>
49
+ </div>
50
+ <div class="show">
51
+ <label>Email:</label>
52
+ <span class="value">johndoe@example.com</span>
53
+ </div>
54
+ <div class="show">
55
+ <label>Born on:</label>
56
+ <span class="value">**censored**</span>
57
+ </div>
58
+ <br clear="all">
59
+ </div>
60
+
61
+ == Options:
62
+
63
+ There are a number of options that control the resulting HTML.
64
+ See lib/simple_show.rb for what they are. For example,
65
+ if you don't want that BR tag then set SimpleShow.clear_on_close to false.
66
+
67
+ == TODO:
68
+
69
+ - Add support for associations.
70
+ - Add options for formatting dates, numerics, decimals, and booleans.
71
+
72
+ == License:
73
+
74
+ (The MIT License)
75
+
76
+ Copyright (c) 2011 Philip Hallstrom <philip@pjkh.com>
77
+
78
+ Permission is hereby granted, free of charge, to any person obtaining
79
+ a copy of this software and associated documentation files (the
80
+ 'Software'), to deal in the Software without restriction, including
81
+ without limitation the rights to use, copy, modify, merge, publish,
82
+ distribute, sublicense, and/or sell copies of the Software, and to
83
+ permit persons to whom the Software is furnished to do so, subject to
84
+ the following conditions:
85
+
86
+ The above copyright notice and this permission notice shall be
87
+ included in all copies or substantial portions of the Software.
88
+
89
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
90
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
91
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
92
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
93
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
94
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
95
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib'
7
+ test.libs << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
@@ -0,0 +1,16 @@
1
+ module SimpleShow
2
+ module ApplicationHelper
3
+ def simple_show_for(record, options = {}, &block)
4
+ raise ArgumentError, "Missing block" unless block_given?
5
+
6
+ options[:html] ||= {}
7
+ options[:html][:id] ||= dom_id(record)
8
+ options[:html][:class] = "#{SimpleShow.show_class} #{dom_class(record)} #{options[:html][:class]}".strip
9
+
10
+ output = capture(SimpleShow::Base.new(self, record, options), &block)
11
+ output.concat tag(:br, :clear => 'all') if SimpleShow.clear_on_close
12
+
13
+ content_tag(:div, output, :id => options[:html][:id], :class => options[:html][:class])
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ To copy a SimpleShow initializer to your Rails App, with some configuration values, just do:
2
+
3
+ rails generate simple_show:install
@@ -0,0 +1,13 @@
1
+ module SimpleShow
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copy SimpleShow configuration"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def copy_config
8
+ directory 'config'
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # Use this setup block to configure all options available in SimpleShow.
2
+ SimpleShow.setup do |config|
3
+
4
+ #config.show_class = :simple_show
5
+ #config.wrapper_tag = :div
6
+ #config.wrapper_class = :show
7
+ #config.label_tag = :label
8
+ #config.label_class = nil
9
+ #config.label_prefix = nil
10
+ #config.label_suffix = ':'
11
+ #config.value_tag = :span
12
+ #config.value_class = :value
13
+ #config.value_prefix = nil
14
+ #config.value_suffix = nil
15
+ #config.clear_on_close = true
16
+
17
+ end
@@ -0,0 +1,36 @@
1
+ module SimpleShow
2
+ class Base
3
+ def initialize(binding, record, options = {})
4
+ @binding = binding
5
+ @record = record
6
+ @options = options
7
+ end
8
+
9
+ def show(attr, options = {}, &block)
10
+ output = label(attr, options, &block)
11
+ output += value(attr, options, &block)
12
+ if SimpleShow.wrapper_tag.nil?
13
+ @binding.output_buffer += output
14
+ else
15
+ @binding.output_buffer += @binding.content_tag(SimpleShow.wrapper_tag, output, :class => SimpleShow.wrapper_class)
16
+ end
17
+ end
18
+
19
+ def label(attr, options = {}, &block)
20
+ @binding.content_tag(SimpleShow.label_tag, :class => SimpleShow.label_class) do
21
+ [SimpleShow.label_prefix, options[:label] || @record.class.human_attribute_name(attr), SimpleShow.label_suffix].compact.join
22
+ end
23
+ end
24
+
25
+ def value(attr, options = {}, &block)
26
+ if block_given?
27
+ value = yield(@record)
28
+ else
29
+ value = options[:value] || @record.send(attr)
30
+ end
31
+ @binding.content_tag(SimpleShow.value_tag, :class => SimpleShow.value_class) do
32
+ [SimpleShow.value_prefix, value, SimpleShow.value_suffix].compact.join
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ module SimpleShow
2
+ class Engine < Rails::Engine
3
+ initializer "simple_show.setup" do
4
+ config.to_prepare do
5
+ ApplicationController.helper(::SimpleShow::ApplicationHelper)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleShow
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'action_view'
2
+ require 'simple_show/base'
3
+
4
+ if defined?(::Rails::Railtie)
5
+ require 'simple_show/railtie'
6
+ end
7
+
8
+ module SimpleShow
9
+
10
+ mattr_accessor :show_class
11
+ @@show_class = :simple_show
12
+
13
+ mattr_accessor :wrapper_tag
14
+ @@wrapper_tag = :div
15
+
16
+ mattr_accessor :wrapper_class
17
+ @@wrapper_class = :show
18
+
19
+ mattr_accessor :label_tag
20
+ @@label_tag = :label
21
+
22
+ mattr_accessor :label_class
23
+ @@label_class = nil
24
+
25
+ mattr_accessor :label_prefix
26
+ @@label_prefix = nil
27
+
28
+ mattr_accessor :label_suffix
29
+ @@label_suffix = ':'
30
+
31
+ mattr_accessor :value_tag
32
+ @@value_tag = :span
33
+
34
+ mattr_accessor :value_class
35
+ @@value_class = :value
36
+
37
+ mattr_accessor :value_prefix
38
+ @@value_prefix = nil
39
+
40
+ mattr_accessor :value_suffix
41
+ @@value_suffix = nil
42
+
43
+ mattr_accessor :clear_on_close
44
+ @@clear_on_close = true
45
+
46
+ # Default way to setup SimpleShow. Run rails generate simple_show:install
47
+ # to create a fresh initializer with all configuration values.
48
+ def self.setup
49
+ yield self
50
+ end
51
+
52
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "simple_show/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "simple_show"
7
+ s.version = SimpleShow::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Philip Hallstrom"]
10
+ s.email = ["philip@pjkh.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{SimpleShow is #new what SimpleForm is to #edit}
13
+ s.description = %q{SimpleShow is #new what SimpleForm is to #edit}
14
+
15
+ s.rubyforge_project = "simple_show"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'nokogiri'
4
+
5
+ require 'test/unit'
6
+ require 'pp'
7
+
8
+ require 'active_record'
9
+ require 'active_model'
10
+ require 'action_controller'
11
+ require 'action_view'
12
+ require 'action_view/base'
13
+ require 'action_view/template'
14
+
15
+ require 'simple_show'
16
+ require 'app/helpers/simple_show/application_helper'
17
+
18
+ ################################################################################
19
+
20
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
21
+ ActiveRecord::Migration.verbose = false
22
+
23
+ ActiveRecord::Schema.define(:version => 1) do
24
+ create_table :golfers do |t|
25
+ t.column :name, :string
26
+ t.column :phone, :string
27
+ t.column :email, :string
28
+ t.column :born_on, :date
29
+ t.column :is_left_handed, :boolean
30
+ t.column :handicap, :decimal, :scale => 3, :precision => 1
31
+ t.timestamps
32
+ end
33
+ end
34
+
35
+ ################################################################################
36
+
37
+ class Golfer < ActiveRecord::Base
38
+ end
39
+
40
+ ################################################################################
41
+
42
+ class SimpleShowTestCase < ActiveSupport::TestCase
43
+ include SimpleShow::ApplicationHelper
44
+ include ActionController::RecordIdentifier
45
+ include ActionView::Helpers::CaptureHelper
46
+ include ActionView::Helpers::TagHelper
47
+ attr_accessor :output_buffer
48
+
49
+ def setup
50
+ @philip = Golfer.create!(
51
+ :name => 'Philip Hallstrom',
52
+ :phone => '3604801209',
53
+ :email => 'philip@pjkh.com',
54
+ :born_on => Date.civil(1974, 5, 24),
55
+ :is_left_handed => true,
56
+ :handicap => 6.5
57
+ )
58
+ end
59
+ end
60
+
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class SimpleShowTest < SimpleShowTestCase
4
+
5
+ test 'setup block yields self' do
6
+ SimpleShow.setup do |config|
7
+ assert_equal SimpleShow, config
8
+ end
9
+ end
10
+
11
+ test 'default configuration' do
12
+ assert_equal :simple_show , SimpleShow.show_class
13
+ assert_equal :div , SimpleShow.wrapper_tag
14
+ assert_equal :show , SimpleShow.wrapper_class
15
+ assert_equal :label , SimpleShow.label_tag
16
+ assert_equal nil , SimpleShow.label_class
17
+ assert_equal nil , SimpleShow.label_prefix
18
+ assert_equal ':' , SimpleShow.label_suffix
19
+ assert_equal :span , SimpleShow.value_tag
20
+ assert_equal :value , SimpleShow.value_class
21
+ assert_equal nil , SimpleShow.value_prefix
22
+ assert_equal nil , SimpleShow.value_suffix
23
+ assert_equal true , SimpleShow.clear_on_close
24
+ end
25
+
26
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class SimpleShowForTest < SimpleShowTestCase
4
+
5
+ test 'simple_show_for raises error without a block' do
6
+ assert_raise ArgumentError do
7
+ simple_show_for
8
+ end
9
+ end
10
+
11
+ test 'html wrapper includes the correct id and class' do
12
+ doc = Nokogiri::HTML.fragment(simple_show_for @philip do |f|
13
+ ''
14
+ end)
15
+ assert_equal %w[simple_show golfer], doc.at('div').attr('class').split(/\s+/)
16
+ assert_equal "golfer_#{@philip.id}", doc.at('div').attr('id')
17
+ end
18
+
19
+ test 'last tag is a BR that clears all when enabled' do
20
+ clear_on_close = SimpleShow::clear_on_close
21
+ SimpleShow::clear_on_close = true
22
+ doc = Nokogiri::HTML.fragment(simple_show_for @philip do |f|
23
+ ''
24
+ end)
25
+ SimpleShow::clear_on_close = clear_on_close
26
+ assert_equal 'br', doc.at('div').elements.last.name
27
+ assert_equal 'all', doc.at('div').elements.last.attr('clear')
28
+ end
29
+
30
+ test 'last tag is not a BR when disabled' do
31
+ clear_on_close = SimpleShow::clear_on_close
32
+ SimpleShow::clear_on_close = false
33
+ doc = Nokogiri::HTML.fragment(simple_show_for @philip do |f|
34
+ ''
35
+ end)
36
+ assert_not_equal 'br', doc.at('div').elements.last.try(:name)
37
+ SimpleShow::clear_on_close = clear_on_close
38
+ end
39
+
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class SimpleShowLabelTest < SimpleShowTestCase
4
+
5
+ test 'labels and label overrides' do
6
+ doc = Nokogiri::HTML.fragment(simple_show_for @philip do |f|
7
+ f.show :name
8
+ f.show :phone, :label => 'Cell Phone'
9
+ end)
10
+ assert_equal 'Name:', doc.css('label').first.text
11
+ assert_equal 'Cell Phone:', doc.css('label').last.text
12
+ end
13
+
14
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class SimpleShowValueTest < SimpleShowTestCase
4
+
5
+ test 'values and value overrides' do
6
+ doc = Nokogiri::HTML.fragment(simple_show_for @philip do |f|
7
+ f.show :name
8
+ f.show(:email) {|o| o.email.upcase }
9
+ f.show :phone, :value => '867-5309'
10
+ end)
11
+ assert_equal 'Philip Hallstrom', doc.css('span.value')[0].text
12
+ assert_equal 'PHILIP@PJKH.COM', doc.css('span.value')[1].text
13
+ assert_equal '867-5309', doc.css('span.value')[2].text
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_show
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Philip Hallstrom
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-01 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: "SimpleShow is #new what SimpleForm is to #edit"
23
+ email:
24
+ - philip@pjkh.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - .rvmrc
34
+ - Gemfile
35
+ - README.rdoc
36
+ - Rakefile
37
+ - app/helpers/simple_show/application_helper.rb
38
+ - lib/generators/simple_show/USAGE
39
+ - lib/generators/simple_show/install_generator.rb
40
+ - lib/generators/simple_show/templates/config/initializers/simple_show.rb
41
+ - lib/simple_show.rb
42
+ - lib/simple_show/base.rb
43
+ - lib/simple_show/railtie.rb
44
+ - lib/simple_show/version.rb
45
+ - simple_show.gemspec
46
+ - test/test_helper.rb
47
+ - test/test_simple_show.rb
48
+ - test/test_simple_show_for.rb
49
+ - test/test_simple_show_label.rb
50
+ - test/test_simple_show_value.rb
51
+ has_rdoc: true
52
+ homepage: ""
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: simple_show
81
+ rubygems_version: 1.4.2
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: "SimpleShow is #new what SimpleForm is to #edit"
85
+ test_files:
86
+ - test/test_helper.rb
87
+ - test/test_simple_show.rb
88
+ - test/test_simple_show_for.rb
89
+ - test/test_simple_show_label.rb
90
+ - test/test_simple_show_value.rb