easy_presenter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f55e09c891f829eb8db5291671fcae84347b2701
4
+ data.tar.gz: 55030a1f38b0ae43c7bf7f7febdfdd834f2bf601
5
+ SHA512:
6
+ metadata.gz: f934578a6067f482cca8a5e21bbc8325ad0f5fb2134d441692171784e9c0ababce61d16c50f6ca2a131f5a7dac0424527ba6731dceedfbb66db2599bc42051df
7
+ data.tar.gz: d1af8a7912d2e3279ddedafcbf4b864b91fcffc47f6bd889011ceef281adb4fe6cb0f9bb63bcf14a6d262230b8a00b6f4e8d66b38b78a90a7a352f66c0344cea
@@ -0,0 +1,18 @@
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
18
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_presenter.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Steven Chung
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.
@@ -0,0 +1,67 @@
1
+ # EasyPresenter
2
+
3
+ Access view methods from the context of your model.
4
+
5
+ ## Usage
6
+ ### Easy
7
+ ```
8
+ gem 'easy_presenter'
9
+ ```
10
+
11
+ Given a model `User`
12
+ ```ruby
13
+ # app/presenters/user_presenter.rb
14
+ module UserPresenter
15
+ # Separate view of the model in the presenter, even if it's a `String`. Leave data manipulation in the model.
16
+ def full_name
17
+ "#{first_name} #{last_name}"
18
+ end
19
+
20
+ # Quick link to the `User`
21
+ def link
22
+ link_to full_name, user_path(self)
23
+ end
24
+
25
+ # Using a module, so you must do this to add class methods
26
+ module ClassMethods
27
+ # String of all full names separated by commas
28
+ def full_names
29
+ # better to access `User` this way, you may use `User` instead of `self.class` though.
30
+ self.class.all.map(&:full_name).join(", ")
31
+ end
32
+ end
33
+ end
34
+ ```
35
+
36
+ Access your presenter methods in a view:
37
+ ```erb
38
+ <!-- app/views/users/index.html.erb -->
39
+ <% User.all.each do |user| %>
40
+ <%= user.link %><br>
41
+ <% end %>
42
+ ```
43
+
44
+ Or a helper:
45
+ ```ruby
46
+ # app/helpers/users_helper.rb
47
+ module UsersHelper
48
+ def full_names_in_div
49
+ content_tag :div, User.full_names
50
+ end
51
+ end
52
+ ```
53
+
54
+ ### Supported ORMs
55
+ `ActiveRecord`, `Mongoid`, you may add support to an ORM at the bottom of `lib/easy_presenter.rb` and send in a pull request if you wish.
56
+
57
+ ### Advanced
58
+ You may use the following modules to add EasyPresenter to other classes.
59
+
60
+ * `EasyPresenter::Base` - add access to view methods
61
+ * `EasyPresenter` - `EasyPresenter::Base` and if `method_missing?`, give access to the `ClassNamePresenter` class
62
+
63
+ ## Credits
64
+ Extracted out of [Placemark](https://www.placemarkhq.com/).
65
+
66
+ ### Contribution
67
+ Feel free to fork, post issues or send any pull requests. Thanks.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_presenter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_presenter"
8
+ spec.version = EasyPresenter::VERSION
9
+ spec.authors = ["s12chung"]
10
+ spec.email = ["steve.chung7@gmail.com"]
11
+ spec.description = "Access view methods in your models."
12
+ spec.summary = "Access view methods in your models."
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
+
24
+ spec.add_dependency 'request_store'
25
+ end
@@ -0,0 +1,33 @@
1
+ require "easy_presenter/version"
2
+ require "easy_presenter/base"
3
+ require "easy_presenter/railtie"
4
+
5
+ module EasyPresenter
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ presenter_class = "#{self}Presenter".safe_constantize
10
+ if presenter_class
11
+ include Base
12
+ presenter_class.extend ActiveSupport::Concern
13
+ include presenter_class
14
+ end
15
+ end
16
+
17
+ # for the Presenter module only
18
+ class << self
19
+ def controller
20
+ RequestStore.store[:presenter_controller]
21
+ end
22
+
23
+ def controller=(presenter_controller)
24
+ RequestStore.store[:presenter_controller] = presenter_controller
25
+ end
26
+ end
27
+ end
28
+
29
+ if defined?(ActiveRecord::Base)
30
+ ActiveRecord::Base.send :include, EasyPresenter
31
+ elsif defined?(Mongoid::Document)
32
+ Mongoid::Document.send :include, EasyPresenter
33
+ end
@@ -0,0 +1,55 @@
1
+ module EasyPresenter
2
+ module Base
3
+ extend ActiveSupport::Concern
4
+
5
+ def controller_get(instance_variable)
6
+ self.class.controller_get instance_variable
7
+ end
8
+
9
+ # See https://github.com/amatsuda/active_decorator/blob/master/lib/active_decorator/helpers.rb
10
+ def method_missing(name, *args, &block)
11
+ super
12
+ rescue NoMethodError, NameError => original_error
13
+ self.class.try_helper(original_error, name, *args, &block)
14
+ end
15
+
16
+ # for Rails #url_for helpers
17
+ def url_options
18
+ controller = EasyPresenter.controller
19
+ if controller
20
+ controller.url_options
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ module ClassMethods
27
+ def controller_get(instance_variable)
28
+ controller.instance_variable_get instance_variable
29
+ end
30
+
31
+ def method_missing(name, *args, &block)
32
+ super
33
+ rescue NoMethodError, NameError => original_error
34
+ try_helper(original_error, name, *args, &block)
35
+ end
36
+
37
+ def try_helper(original_error, name, *args, &block)
38
+ controller = EasyPresenter.controller
39
+ if controller
40
+ begin
41
+ controller.view_context.send name, *args, &block
42
+ rescue NoMethodError, NameError => new_error
43
+ if new_error.name == name && new_error.args == args
44
+ raise original_error
45
+ else
46
+ raise new_error
47
+ end
48
+ end
49
+ else
50
+ raise original_error
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ module EasyPresenter
2
+ module Filter
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ append_before_filter :set_easy_presenter_controller
7
+ end
8
+
9
+ protected
10
+ def set_easy_presenter_controller
11
+ EasyPresenter.controller = self
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ require "easy_presenter/filter"
2
+ require "rails"
3
+
4
+ module EasyPresenter
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'easy_presenter' do
7
+ ActiveSupport.on_load(:action_controller) do
8
+ ActionController::Base.send :include, Filter
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module EasyPresenter
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_presenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - s12chung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-13 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
+ - !ruby/object:Gem::Dependency
42
+ name: request_store
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Access view methods in your models.
56
+ email:
57
+ - steve.chung7@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - easy_presenter.gemspec
68
+ - lib/easy_presenter.rb
69
+ - lib/easy_presenter/base.rb
70
+ - lib/easy_presenter/filter.rb
71
+ - lib/easy_presenter/railtie.rb
72
+ - lib/easy_presenter/version.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Access view methods in your models.
97
+ test_files: []