active_path-view_injection 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 970eba4c3d5a9ccaf4a6268efefe1fb56f019fa0
4
+ data.tar.gz: 0d9787274fcb67398fba02b3e430f9e008022574
5
+ SHA512:
6
+ metadata.gz: cbc7a68086c86b838106116dde827b2ae0e6faedc04022ee32a0035d35208d1ca1ed63125bc39fc8b11c79158f2364ad2ab2d3523d9e729909e31a5134c3ad31
7
+ data.tar.gz: '038734bd1bd82e046997a3fbba0e2858db348d1c01edd084c27bbd639106e9d93bee908b2c0b13aeca3c7003921197e20e288c34b97f52026f032752692e4a29'
@@ -0,0 +1 @@
1
+ .idea
@@ -0,0 +1,82 @@
1
+ # ActivePath View Injection:
2
+ View injection for your partials.
3
+
4
+
5
+ ## Installation
6
+
7
+ Add to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_path-view_injection'
11
+ ```
12
+
13
+ Add the initializer `config/initializers/active_path.rb` and enable `ActivePath`:
14
+
15
+ ```ruby
16
+
17
+ ActivePath.configure do |config|
18
+
19
+ config.enabled = true
20
+
21
+ end
22
+
23
+ ```
24
+
25
+
26
+ **View injection:**
27
+
28
+ ```ruby
29
+
30
+ ActivePath.configure do |config|
31
+
32
+ ...
33
+
34
+ config.partials.prepend('pages/content').with('example/test')
35
+
36
+ end
37
+
38
+ ```
39
+
40
+ The above example assumes your application renders a partial called 'pages/content' and you want to `prepend` a partial called 'example/test'.
41
+
42
+ Your partial will have the same local parameters access as the prepended partial. You can also use `append`:
43
+
44
+ ```ruby
45
+
46
+ config.partials.append('pages/content').with('example/test')
47
+
48
+ ```
49
+
50
+
51
+ The above renders 'example/test' after 'pages/content'.
52
+
53
+ --
54
+
55
+ **Conditions:**
56
+
57
+ The `when` method allows you to conditionally inject your view. Consider this partial:
58
+
59
+ ```ruby
60
+ render partial: 'pages/content', locals: { page_id: 9 }
61
+ ```
62
+
63
+ You can pass in a hash which must match the local variables.
64
+
65
+ ```ruby
66
+
67
+ config.partials.append('pages/content').with('example/test').when(page_id: 9)
68
+
69
+ ```
70
+
71
+ Or pass in a block for more flexibility with your conditions:
72
+
73
+ ```ruby
74
+
75
+ config.partials.append('pages/content').with('example/test').when do |locals|
76
+ locals[:page_id] == 9
77
+ end
78
+
79
+ ```
80
+
81
+
82
+ Feel free to [submit issues](https://github.com/active-path/view-injection/issues) or [help make it better](https://github.com/active-path/view-injection/pulls).
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
4
+ require 'active_path/view_injection/version'
5
+
6
+ desc "Release version #{ActivePath::ViewInjection::VERSION} of the gem"
7
+ task :release do
8
+
9
+ system "git tag -a v#{ActivePath::ViewInjection::VERSION} -m 'Tagging #{ActivePath::ViewInjection::VERSION}'"
10
+ system 'git push --tags'
11
+
12
+ system "gem build active_path-view_injection.gemspec"
13
+ system "gem push active_path-view_injection-#{ActivePath::ViewInjection::VERSION}.gem"
14
+ system "rm active_path-view_injection-#{ActivePath::ViewInjection::VERSION}.gem"
15
+ end
@@ -0,0 +1,17 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'active_path/view_injection/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'active_path-view_injection'
6
+ s.version = ActivePath::ViewInjection::VERSION
7
+ s.date = '2018-04-28'
8
+ s.summary = "ActivePath View Injection"
9
+ s.description = "Inject partials before or after other partials!"
10
+ s.authors = ["Ryan Tulino"]
11
+ s.email = 'rtulino@gmail.com'
12
+ s.files = `git ls-files`.split("\n")
13
+ s.homepage = 'http://rubygems.org/gems/active_path-view_injection'
14
+ s.required_ruby_version = '>= 2.2.0'
15
+ s.add_dependency 'rails', '~> 5'
16
+ s.add_dependency 'active_path', '~> 0.1.0'
17
+ end
@@ -0,0 +1 @@
1
+ require 'active_path/view_injection/engine'
@@ -0,0 +1,31 @@
1
+ module ActivePath
2
+ module ViewInjection
3
+ module Conditions
4
+ module Conditional
5
+ def conditions_match?(conditions)
6
+ conditions.all? do |condition|
7
+ condition_match?(condition)
8
+ end
9
+ end
10
+
11
+ def condition_match?(condition)
12
+ if condition.is_a?(Hash)
13
+ hash_match?(condition)
14
+ elsif condition.is_a?(Proc)
15
+ proc_match?(condition)
16
+ end
17
+ end
18
+
19
+ def hash_match?(condition)
20
+ condition.all? do |k,v|
21
+ options[:locals][k] == v
22
+ end
23
+ end
24
+
25
+ def proc_match?(condition)
26
+ condition.call(options[:locals])
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module ActivePath
2
+ module ViewInjection
3
+ module Configuration
4
+ class Attachment
5
+ attr_reader :partial, :conditions
6
+
7
+ def initialize(partial)
8
+ @partial = partial
9
+ @conditions = []
10
+ end
11
+
12
+ def when(condition = nil, &block)
13
+ @conditions << (condition || block)
14
+ self
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_path/view_injection/configuration/attachment'
2
+ module ActivePath
3
+ module ViewInjection
4
+ module Configuration
5
+ class Partial
6
+ attr_reader :attachments
7
+
8
+ def initialize
9
+ @attachments = []
10
+ end
11
+
12
+ def with(partial_name)
13
+ @attachments << Attachment.new(partial_name)
14
+ @attachments.last
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_path/view_injection/configuration/partial'
2
+ module ActivePath
3
+ module ViewInjection
4
+ module Configuration
5
+ class Partials
6
+ attr_reader :partials
7
+
8
+ def initialize
9
+ @partials = {}
10
+ end
11
+
12
+ def prepend(key)
13
+ partial(key, true)
14
+ end
15
+
16
+ def append(key)
17
+ partial(key, false)
18
+ end
19
+
20
+ private
21
+
22
+ def partial(key, prepend = true)
23
+ @partials["#{key}/#{prepend}"] ||= Partial.new
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_path/view_injection/configuration/partials'
2
+ require 'active_path/view_injection/subscriber'
3
+ module ActivePath
4
+ module ViewInjection
5
+ class Engine < Rails::Engine
6
+ isolate_namespace ActivePath::ViewInjection
7
+
8
+ config.before_configuration do
9
+ config.active_path.partials = Configuration::Partials.new
10
+ end
11
+
12
+ config.after_initialize do
13
+ ActiveSupport::Notifications.subscribe(:render_partial, Subscriber.new)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_path/view_injection/conditions/conditional'
2
+ require 'active_path/subscribers/subscriber'
3
+ module ActivePath
4
+ module ViewInjection
5
+ class Subscriber < Subscribers::Subscriber
6
+ include Conditions::Conditional
7
+
8
+ def perform
9
+ prepend_attachments.each do |attachment|
10
+ buffer.prepend(render_attachment(attachment))
11
+ end
12
+
13
+ append_attachments.each do |attachment|
14
+ buffer.concat(render_attachment(attachment))
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def append_attachments
21
+ attachments(ActivePath.config.partials.append(partial))
22
+ end
23
+
24
+ def prepend_attachments
25
+ attachments(ActivePath.config.partials.prepend(partial))
26
+ end
27
+
28
+ def attachments(configuration)
29
+ configuration.attachments.select do |attachment|
30
+ conditions_match?(attachment.conditions)
31
+ end
32
+ end
33
+
34
+ def render_attachment(attachment)
35
+ hash = { partial: attachment.partial }
36
+ options_hash = options.is_a?(Hash) ? options.merge(hash) : hash
37
+ context.render(options_hash)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module ActivePath
2
+ module ViewInjection
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_path-view_injection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Tulino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-28 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: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_path
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ description: Inject partials before or after other partials!
42
+ email: rtulino@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - README.md
49
+ - Rakefile
50
+ - active_path-view_injection.gemspec
51
+ - lib/active_path/view_injection.rb
52
+ - lib/active_path/view_injection/conditions/conditional.rb
53
+ - lib/active_path/view_injection/configuration/attachment.rb
54
+ - lib/active_path/view_injection/configuration/partial.rb
55
+ - lib/active_path/view_injection/configuration/partials.rb
56
+ - lib/active_path/view_injection/engine.rb
57
+ - lib/active_path/view_injection/subscriber.rb
58
+ - lib/active_path/view_injection/version.rb
59
+ homepage: http://rubygems.org/gems/active_path-view_injection
60
+ licenses: []
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.2.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.11
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: ActivePath View Injection
82
+ test_files: []