simple_display 0.0.1.alpha

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ca2b12251ad6518748638eef041b252158550248
4
+ data.tar.gz: 4fa6413876069b53ca43c9c264329c24c59c2a56
5
+ SHA512:
6
+ metadata.gz: 44788351ca7d6ae2b78d17a5ad434bd0bf595841a3f8126ef520cc049d2cbc713402dd9fb91e36a669f1cb156f7deca407f11e3b37b95a67bbac5ca6a8adf0cc
7
+ data.tar.gz: 8b044500cf8e51a91c050d91577a936d25aa634db50d33e264e8aee86d8acc7e7de21618ed666b3fd2c4235c4df80adad65de7737c609ec8cb8cfca372907ebc
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 simple_display.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Marc Riera
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,65 @@
1
+ # SimpleDisplay
2
+
3
+ Showcase your Rails models easily.
4
+
5
+ **SimpleDisplay** aims to provide a flexible way to display your model
6
+ attribute and methods, so you can move from this:
7
+
8
+ ```erb
9
+ <dl>
10
+ <% if @book.title.present? %>
11
+ <dt>Title</dt>
12
+ <dd><%= @example.title %></dd>
13
+ <% end %>
14
+
15
+ <% if @example.price.present? %>
16
+ <dt>Price</dt>
17
+ <dd><%= number_with_currency @example.price %></dd>
18
+ <% end %>
19
+ </dl>
20
+ ```
21
+
22
+ To this:
23
+
24
+ ```erb
25
+ <%= display_for @example do |d| %>
26
+ <dl>
27
+ <%= d.display :title %>
28
+ <%= d.currency :price %>
29
+ </dl>
30
+ <% end %>
31
+ ```
32
+
33
+ This gem was inspired by [this
34
+ post](http://quickleft.com/blog/drying-your-views-with-dsl-s?utm_source=rubyweekly&utm_medium=email)
35
+ by Ben West in the Quick Left blog. Thank you! <3
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ gem 'simple_display', github: 'mrcasals/simple_display'
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ ## Usage
48
+
49
+ *TODO: add some content here! Meanwhile, check the introduction of the README
50
+ file.*
51
+
52
+ ## TODO
53
+
54
+ * Add tests & docs (sorry!)
55
+ * Add more displayers
56
+ * Add more content to this README file
57
+ * Let users use custom displayers
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module SimpleDisplay
2
+ module ActionViewExtensions
3
+ module DisplayHelper
4
+ def display_for(model, &block)
5
+ capture(SimpleDisplay::DisplayBlock.new(model, self), &block)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/inflector'
2
+
3
+ module SimpleDisplay
4
+ class DisplayBlock
5
+ attr_reader :model, :helper
6
+
7
+ def initialize(model, helper)
8
+ @model, @helper = model, helper
9
+ end
10
+
11
+ def display(field, label = nil, &block)
12
+ base_displayer = SimpleDisplay::Displayers::Base.new(model, helper)
13
+ base_displayer.display(field, label, &block)
14
+ end
15
+
16
+ def method_missing(method, *args, &block)
17
+ klass = "simple_display/displayers/#{method}_displayer".camelize
18
+ if Module.const_get(klass).is_a?(Class)
19
+ klass = klass.constantize
20
+ klass.new(model, helper).display(*args)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def respond_to?(method, *args, &block)
27
+ klass = "simple_display/displayers/#{method}_displayer".camelize
28
+ Module.const_get(klass).is_a?(Class) || super
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,37 @@
1
+ module SimpleDisplay
2
+ module Displayers
3
+ class Base
4
+ attr_reader :model, :helper
5
+
6
+ def initialize(model, helper)
7
+ @model = model
8
+ @helper = helper
9
+ end
10
+
11
+ def display(field, label = nil, &block)
12
+ field_value = model.send(field)
13
+ if field_value.present?
14
+ content = value(field_value, &block)
15
+
16
+ line(disp_label(field, label), content.to_s)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def value(field_value, &block)
23
+ return helper.capture(field_value, &block) if block_given?
24
+ field_value
25
+ end
26
+
27
+ def disp_label(field, label)
28
+ label || model.class.human_attribute_name(field)
29
+ end
30
+
31
+ def line(dt, dd)
32
+ helper.content_tag(:dt, dt) +
33
+ helper.content_tag(:dd, dd)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ module SimpleDisplay
2
+ module Displayers
3
+ class CurrencyDisplayer < Base
4
+ def display(field, label = nil, &block)
5
+ super(field, label) do |f|
6
+ currency = helper.number_to_currency value(model.send(field), &block)
7
+ helper.content_tag(:strong, currency)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module SimpleDisplay
2
+ module Displayers
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :Base
6
+ autoload :CurrencyDisplayer
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails/railtie'
2
+ require 'simple_display/action_view_extensions/display_helper'
3
+
4
+ module SimpleDisplay
5
+ class Railtie < Rails::Railtie
6
+ config.eager_load_namespaces << SimpleDisplay
7
+
8
+ initializer 'simple_display.action_view_extensions.display_helper' do
9
+ ActionView::Base.send :include, SimpleDisplay::ActionViewExtensions::DisplayHelper
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleDisplay
2
+ VERSION = "0.0.1.alpha"
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'simple_display/version'
2
+ require 'action_view'
3
+ require 'simple_display/display_block'
4
+ require 'simple_display/action_view_extensions/display_helper'
5
+
6
+ module SimpleDisplay
7
+ extend ActiveSupport::Autoload
8
+
9
+ eager_autoload do
10
+ autoload :Displayers
11
+ end
12
+ end
13
+
14
+ require 'simple_display/railtie' if defined?(Rails)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_display/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_display"
8
+ spec.version = SimpleDisplay::VERSION
9
+ spec.authors = ["Marc Riera"]
10
+ spec.email = ["mrc2407@gmail.com"]
11
+ spec.description = %q{Write a gem description}
12
+ spec.summary = %q{Write a gem summary}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_display
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Marc Riera
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Write a gem description
42
+ email:
43
+ - mrc2407@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/simple_display.rb
54
+ - lib/simple_display/action_view_extensions/display_helper.rb
55
+ - lib/simple_display/display_block.rb
56
+ - lib/simple_display/displayers.rb
57
+ - lib/simple_display/displayers/base.rb
58
+ - lib/simple_display/displayers/currency_displayer.rb
59
+ - lib/simple_display/railtie.rb
60
+ - lib/simple_display/version.rb
61
+ - simple_display.gemspec
62
+ homepage: ''
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>'
78
+ - !ruby/object:Gem::Version
79
+ version: 1.3.1
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.0.3
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Write a gem summary
86
+ test_files: []
87
+ has_rdoc: