blush 0.0.1

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: 9343c4323c3c9e3de4f2e747f82652949f9b1f6f
4
+ data.tar.gz: a1b4367b03f0ea9b6387a2d8c32bc5d232cac3fc
5
+ SHA512:
6
+ metadata.gz: 20f7cf5815f17d1647948e6f1bb9698248614a4408ccd13940468792ceb6224a8a15df1470fc043ab24d41049c17f584b0bf3389629486e4bcda29a1119cb08f
7
+ data.tar.gz: 72a31b45fd2cda8437d1fd8bac28694c432e087880082c25f3d58a0d59ff80a731e47361f0cf7127daf186f065ad3a195e48a9ddd6e882d4f3f3126ffd755cc1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Grant Colegate
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Blush
2
+
3
+ Make your models fit for presentation. View Models for Rails.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'blush'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ class Comment < ActiveRecord::Base
15
+ has_presenter
16
+ end
17
+
18
+ class CommentPresenter < Blush::Presenter
19
+ def content
20
+ h.simple_format(object.content)
21
+ end
22
+ end
23
+
24
+ # explicitly call the presenter methods
25
+ @comment.presenter.content
26
+
27
+ # delegate to the model if method doesn't exist on presenter
28
+ @comment.present(:content)
29
+ ```
@@ -0,0 +1,46 @@
1
+ require 'blush/configuration'
2
+ require 'blush/has_presenter'
3
+ require 'blush/helper_proxy'
4
+ require 'blush/presenter'
5
+ require 'blush/view_context'
6
+ require 'blush/railtie'
7
+
8
+ ##
9
+ # Wrapper module for Blush
10
+ module Blush
11
+
12
+ ##
13
+ # Convenient API for accessing Blush's configuration
14
+ #
15
+ # Options include:
16
+ # Blush.config.accessor_name to set the default accessor on the model
17
+ # Blush.config.helper_name to set the default helper name on the model
18
+ module_function def config
19
+ @config ||= Blush::Configuration.new
20
+ end
21
+
22
+ ##
23
+ # Cache the helpers globally
24
+ module_function def helpers=(helper_proxy)
25
+ @helpers = helper_proxy
26
+ end
27
+
28
+ ##
29
+ # Get the cached helpers
30
+ module_function def helpers
31
+ @helpers ||= Blush::HelperProxy.new
32
+ end
33
+
34
+ ##
35
+ # Cache the view context globally
36
+ module_function def view_context=(view_context)
37
+ @view_context = view_context
38
+ end
39
+
40
+ ##
41
+ # Get the cached view context
42
+ module_function def view_context
43
+ @view_context ||= ApplicationController.new.view_context
44
+ end
45
+
46
+ end
@@ -0,0 +1,14 @@
1
+ module Blush
2
+
3
+ ##
4
+ # Class to store configuration for Blush
5
+ class Configuration
6
+ attr_accessor :accessor_name, :helper_name
7
+
8
+ def initialize
9
+ @accessor_name = :presenter
10
+ @helper_name = :present
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,38 @@
1
+ module Blush
2
+
3
+ ##
4
+ # Tell a model that it has a presenter
5
+ #
6
+ # This is included in all models
7
+ #
8
+ # Use with defaults:
9
+ # has_presenter
10
+ #
11
+ # Use with custom accessor:
12
+ # has_presenter :accessor_name
13
+ #
14
+ # Use with options:
15
+ # has_presenter { class: 'ThingPresenter', helper_name: :foo }
16
+ module HasPresenter
17
+ def has_presenter(accessor_name = {}, options = nil)
18
+ class_attribute :blush_options
19
+
20
+ options, accessor_name = accessor_name, options unless options
21
+ self.blush_options = options
22
+ self.blush_options[:accessor_name] = accessor_name || Blush.config.accessor_name
23
+
24
+ # defines the accessor
25
+ define_method(self.blush_options[:accessor_name]) do
26
+ accessor_name = "@#{self.blush_options[:accessor_name]}"
27
+ presenter_class = (self.blush_options[:class] || "#{self.model_name}Presenter").constantize
28
+ instance_variable_get(accessor_name) || instance_variable_set(accessor_name, presenter_class.new(self))
29
+ end
30
+
31
+ # defines the helper
32
+ define_method(self.blush_options[:helper_name] || Blush.config.helper_name) do |method, *args, &block|
33
+ presenter.try(method, *args, &block) || send(method, *args, &block)
34
+ end
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,11 @@
1
+ module Blush
2
+
3
+ ##
4
+ # Proxy helper methods to the view context
5
+ class HelperProxy
6
+ def method_missing(method, *args, &block)
7
+ Blush.view_context.send(method, *args, &block)
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,37 @@
1
+ module Blush
2
+
3
+ ##
4
+ # The base presenter class. All presenters should inherit from this.
5
+ class Presenter
6
+ attr_accessor :object
7
+
8
+ def initialize(object)
9
+ @object = object
10
+ end
11
+
12
+ ##
13
+ # Alias o to object for convenience
14
+ alias_method :o, :object
15
+
16
+ ##
17
+ # Access helpers from the view context through HelperProxy
18
+ def helpers
19
+ Blush.helpers
20
+ end
21
+
22
+ ##
23
+ # Alias h to helpers for convenience
24
+ alias_method :h, :helpers
25
+
26
+ ##
27
+ # Alias localize to helpers.localize for convenience
28
+ def localize
29
+ helpers.localize
30
+ end
31
+
32
+ ##
33
+ # Alias l to localize for convenience
34
+ alias_method :l, :helpers
35
+ end
36
+
37
+ end
@@ -0,0 +1,29 @@
1
+ module Blush
2
+ class Railtie < Rails::Railtie
3
+ initializer "blush.setup_action_controller" do |app|
4
+ ActiveSupport.on_load :action_controller do
5
+ self.class_eval do
6
+ include Blush::ViewContext
7
+
8
+ before_action :activate_blush
9
+ end
10
+ end
11
+ end
12
+
13
+ initializer "blush.setup_action_mailer" do |app|
14
+ ActiveSupport.on_load :action_mailer do
15
+ self.class_eval do
16
+ include Blush::ViewContext
17
+ end
18
+ end
19
+ end
20
+
21
+ initializer "blush.setup_orm" do |app|
22
+ ActiveSupport.on_load :active_record do
23
+ self.class_eval do
24
+ extend Blush::HasPresenter
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Blush
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ module Blush
2
+
3
+ ##
4
+ # Hooks into controllers and mailers to get the curret view context
5
+ module ViewContext
6
+ ##
7
+ # Taps into the call to view_context to caches it
8
+ def view_context
9
+ super.tap do |context|
10
+ Blush.view_context = context
11
+ end
12
+ end
13
+
14
+ ##
15
+ # Sets the cached view_context
16
+ def activate_blush
17
+ Blush.view_context = view_context
18
+ end
19
+ end
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blush
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Grant Colegate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: View Models for Rails
28
+ email:
29
+ - blaknite@thelanbox.com.au
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/blush.rb
37
+ - lib/blush/configuration.rb
38
+ - lib/blush/has_presenter.rb
39
+ - lib/blush/helper_proxy.rb
40
+ - lib/blush/presenter.rb
41
+ - lib/blush/railtie.rb
42
+ - lib/blush/version.rb
43
+ - lib/blush/view_context.rb
44
+ homepage: ''
45
+ licenses: []
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.2.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: View Models for Rails
67
+ test_files: []
68
+ has_rdoc: