blocks 3.0.2 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +61 -17
  4. data/CHANGELOG.rdoc +27 -0
  5. data/Gemfile +11 -10
  6. data/Guardfile +1 -0
  7. data/README.md +2 -0
  8. data/bin/deploy_docs +1 -1
  9. data/blocks.gemspec +3 -1
  10. data/docs/_includes/configuration.md +3 -6
  11. data/docs/_includes/defining.md +7 -7
  12. data/docs/_includes/{introduction.md → features.md} +2 -2
  13. data/docs/_includes/helper-blocks.md +5 -0
  14. data/docs/_includes/helper-blocks/content_tag.md +44 -0
  15. data/docs/_includes/installation.md +16 -2
  16. data/docs/_includes/overview.md +21 -0
  17. data/docs/_includes/templating.md +1 -1
  18. data/docs/_includes/templating/bootstrap_4_cards.md +9 -9
  19. data/docs/_layouts/slate.html +11 -9
  20. data/docs/index.md +3 -1
  21. data/gemfiles/Gemfile.rails-3-0-stable +1 -0
  22. data/gemfiles/Gemfile.rails-3-1-stable +1 -0
  23. data/gemfiles/Gemfile.rails-3-2-stable +1 -0
  24. data/gemfiles/Gemfile.rails-4-0-stable +2 -1
  25. data/gemfiles/Gemfile.rails-4-1-stable +2 -1
  26. data/gemfiles/Gemfile.rails-4-2-stable +2 -1
  27. data/gemfiles/Gemfile.rails-5-0-stable +4 -3
  28. data/gemfiles/Gemfile.rails-5-1-stable +4 -3
  29. data/gemfiles/Gemfile.rails-5-2-stable +13 -0
  30. data/lib/blocks.rb +37 -29
  31. data/lib/blocks/builders/block_definition.rb +45 -43
  32. data/lib/blocks/builders/builder.rb +96 -60
  33. data/lib/blocks/builders/hook_definition.rb +19 -4
  34. data/lib/blocks/engine.rb +14 -0
  35. data/lib/blocks/helpers/controller_extensions.rb +13 -0
  36. data/lib/blocks/helpers/haml_capture.rb +44 -0
  37. data/lib/blocks/{action_view_extensions → helpers}/view_extensions.rb +10 -4
  38. data/lib/blocks/renderers/adjacent_blocks_renderer.rb +9 -7
  39. data/lib/blocks/renderers/block_placeholder.rb +2 -0
  40. data/lib/blocks/renderers/block_renderer.rb +26 -5
  41. data/lib/blocks/renderers/block_with_hooks_renderer.rb +29 -19
  42. data/lib/blocks/renderers/collection_renderer.rb +18 -6
  43. data/lib/blocks/renderers/nesting_blocks_renderer.rb +9 -11
  44. data/lib/blocks/renderers/partial_renderer.rb +16 -14
  45. data/lib/blocks/renderers/renderer.rb +9 -24
  46. data/lib/blocks/renderers/runtime_context.rb +175 -147
  47. data/lib/blocks/renderers/wrapper_renderer.rb +21 -10
  48. data/lib/blocks/utilities/configurator.rb +30 -6
  49. data/lib/blocks/utilities/hash_with_caller.rb +36 -32
  50. data/lib/blocks/utilities/hash_with_render_strategy.rb +67 -19
  51. data/lib/blocks/utilities/options_set.rb +38 -63
  52. data/lib/blocks/version.rb +3 -1
  53. metadata +23 -22
  54. data/docs/_includes/wip.md +0 -34
  55. data/lib/blocks/experimental/builder_permissions.rb +0 -52
  56. data/lib/blocks/experimental/invalid_permissions_handler.rb +0 -27
  57. data/lib/blocks/renderers/abstract_renderer.rb +0 -69
  58. data/lib/blocks/utilities/dynamic_configuration.rb +0 -71
@@ -1,34 +0,0 @@
1
- <!-- Defining multiple blocks at once -->
2
- ## Multiple Blocks at Once
3
-
4
- ```haml
5
- - namer = Proc.new {|i| "block #{i}" }
6
- blocks.define_each ["a", 1, "b"],
7
- namer do |item|
8
- ="Hello from #{item}"
9
- &br
10
-
11
- = blocks.render("block a")
12
- = blocks.render("block 1")
13
- = blocks.render("block b")
14
- ```
15
-
16
- ```ruby
17
- # where builder is an instance of
18
- # Blocks::Builder
19
- namer = Proc.new {|i| "block #{i}" }
20
- builder.define_each ["a", 1, "b"],
21
- namer do |item|
22
- "Hello from #{item}<br>".html_safe
23
- end
24
-
25
- builder.render("block a") +
26
- builder.render("block 1") +
27
- builder.render("block b")
28
- ```
29
-
30
- Multiple different blocks may be defined at once using the #define_each method.
31
-
32
- #define_each expects at least two parameters: the collection and a proc that can be used to calculate the block name. The proc must at least one parameter, the first being the item in the collection.
33
-
34
- The method also expects either an options hash designating the render strategy or a Ruby block.
@@ -1,52 +0,0 @@
1
- # WIP
2
- module Blocks
3
- module BuilderPermissions
4
- extend ActiveSupport::Concern
5
-
6
- METHODS_TO_PROTECT = [:render, :define, :deferred_render, :replace, :skip]
7
-
8
- included do
9
- attr_writer :permitted_blocks
10
- attr_accessor :allow_all_blocks
11
- attr_accessor :allow_anonymous_blocks
12
-
13
- METHODS_TO_PROTECT.each do |method_name|
14
- alias_method "original_#{method_name}", method_name
15
- define_method(method_name) do |*args, &block|
16
- if permitted?(args.first)
17
- self.send("original_#{method_name}", *args, &block)
18
- else
19
- InvalidPermissionsHandler.build(method_name, args.first)
20
- end
21
- end
22
- end
23
- end
24
-
25
- def restrict_blocks
26
- self.allow_all_blocks = false
27
- end
28
-
29
- def permitted_blocks
30
- @permitted_blocks ||= []
31
- end
32
-
33
- def permit(*names)
34
- self.permitted_blocks += names
35
- end
36
-
37
- def permit_all
38
- self.allow_all_blocks = true
39
- end
40
-
41
- def permit_anonymous_blocks
42
- self.allow_anonymous_blocks = true
43
- end
44
-
45
- def permitted?(name)
46
- allow_all_blocks ||
47
- (block_definitions[name].try(:anonymous) && allow_anonymous_blocks) ||
48
- !name.respond_to?(:to_sym) ||
49
- permitted_blocks.any? {|e| e.to_sym == name.to_sym }
50
- end
51
- end
52
- end
@@ -1,27 +0,0 @@
1
- # WIP
2
- module Blocks
3
- class InvalidPermissionsHandler
4
- LOG = :log
5
- RAISE = :raise
6
-
7
- def self.build(method_name, block_name)
8
- message = "Cannot #{method_name} #{block_name}; #{block_name} is not in the permitted_blocks list"
9
- new(message)
10
- nil
11
- end
12
-
13
- def initialize(message)
14
- send("handle_#{Blocks.invalid_permissions_approach}", message)
15
- end
16
-
17
- private
18
-
19
- def handle_log(message)
20
- Rails.logger.info message
21
- end
22
-
23
- def handle_raise(message)
24
- raise message
25
- end
26
- end
27
- end
@@ -1,69 +0,0 @@
1
- module Blocks
2
- class AbstractRenderer
3
- RENDERERS = [
4
- AdjacentBlocksRenderer,
5
- BlockRenderer,
6
- BlockWithHooksRenderer,
7
- CollectionRenderer,
8
- PartialRenderer,
9
- NestingBlocksRenderer,
10
- WrapperRenderer
11
- ]
12
-
13
- attr_accessor :main_renderer
14
-
15
- delegate :builder, *(RENDERERS.map {|r| r.to_s.demodulize.underscore }), to: :main_renderer
16
- delegate :block_definitions, :block_for, :view, to: :builder
17
- delegate :with_output_buffer, :output_buffer, to: :view
18
-
19
- def initialize(main_renderer=nil)
20
- self.main_renderer = main_renderer
21
- end
22
-
23
- def render(*args)
24
- raise NotImplementedError
25
- end
26
-
27
- def capture(*args, &block)
28
- without_haml_interference do
29
- if block.arity > 0
30
- args = args[0, block.arity]
31
- end
32
-
33
- with_output_buffer do
34
- output_buffer << view.capture(*args, &block)
35
- end
36
- end
37
- end
38
-
39
- protected
40
-
41
- # Complete hack to get around issues with Haml
42
- # Haml does some hacking to ActionView's with_output_buffer and
43
- # output_buffer. In doing so, they make certain assumptions about
44
- # the layout and the view template. The Blocks gem doesn't capture
45
- # blocks immediately but rather stores them for later capturing.
46
- # This can produce an issue if a block that is stored was in Haml
47
- # but the Layout is in ERB. Haml will think that any blocks it
48
- # captures while rendering the layout will be in ERB format. However,
49
- # the block would need to be captured in Haml using a Haml buffer.
50
- # This workaround accomplishes that.
51
- def without_haml_interference(&block)
52
- if defined?(::Haml) && view.instance_variables.include?(:@haml_buffer)
53
- haml_buffer = view.instance_variable_get(:@haml_buffer)
54
- if haml_buffer
55
- was_active = haml_buffer.active?
56
- haml_buffer.active = false
57
- else
58
- haml_buffer = Haml::Buffer.new(nil, Haml::Options.new.for_buffer)
59
- haml_buffer.active = false
60
- kill_buffer = true
61
- view.instance_variable_set(:@haml_buffer, haml_buffer)
62
- end
63
- end
64
- yield
65
- ensure
66
- haml_buffer.active = was_active if haml_buffer
67
- end
68
- end
69
- end
@@ -1,71 +0,0 @@
1
- module Blocks
2
- module DynamicConfiguration
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- def add_config(*attrs)
7
- options = attrs.extract_options!
8
- instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
9
- instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
10
- instance_predicate = options.fetch(:instance_predicate, true)
11
- default_value = options.fetch(:default, nil)
12
-
13
- attrs.each do |name|
14
- define_singleton_method(name) do |value="UNDEFINED"|
15
- if value == "UNDEFINED"
16
- default_value
17
- else
18
- public_send("#{name}=", value)
19
- end
20
- end
21
- define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate
22
-
23
- ivar = "@#{name}"
24
-
25
- define_singleton_method("#{name}=") do |val|
26
- singleton_class.class_eval do
27
- remove_possible_method(name)
28
- define_method(name) do |value="UNDEFINED"|
29
- if value == "UNDEFINED"
30
- val
31
- else
32
- public_send("#{name}=", value)
33
- end
34
- end
35
- end
36
-
37
- class_eval do
38
- remove_possible_method(name)
39
- define_method(name) do
40
- if instance_variable_defined? ivar
41
- instance_variable_get ivar
42
- else
43
- singleton_class.send name
44
- end
45
- end
46
- end
47
- val
48
- end
49
-
50
- if instance_reader
51
- remove_possible_method name
52
- define_method(name) do
53
- if instance_variable_defined?(ivar)
54
- instance_variable_get ivar
55
- else
56
- self.class.public_send name
57
- end
58
- end
59
- define_method("#{name}?") { !!public_send(name) } if instance_predicate
60
- end
61
-
62
- attr_writer name if instance_writer
63
- end
64
- end
65
-
66
- def configure
67
- yield self
68
- end
69
- end
70
- end
71
- end