Class: EditInPlace::Builder
- Inherits:
-
Object
- Object
- EditInPlace::Builder
- Defined in:
- lib/edit_in_place/builder.rb
Overview
Builder is the class that provides the actual functionality to build and render editable content. This class will usually be instantiated in a controller and passed somehow to a view. The view can then use its methods to generate content. Note that when a Builder is created, it’s Configuration is copied from the global configuration.
This class can be extended by utilizing ExtendedBuilder to safely add additional functionality. This can be particularly helpful for edit_in_place extensions that would like to add other content generation methods without requiring the user to yield another builder to the view.
Instance Attribute Summary collapse
-
#config ⇒ Configuration
The Configuration instance that stores the options for this Builder.
Instance Method Summary collapse
-
#configure {|config| ... } ⇒ void
Configures this Builder by yielding its configuration to the given block.
-
#dup ⇒ Builder
Creates a deep copy of this Builder, whose configuration can be safely modified.
- #field(type, *args) ⇒ Object
-
#initialize ⇒ Builder
constructor
Creates a new instance of Builder.
-
#scoped(field_options = {}) {|scoped_builder| ... } ⇒ string
Yields a new, scoped Builder with the given field options.
Constructor Details
#initialize ⇒ Builder
Creates a new instance of EditInPlace::Builder.
24 25 26 |
# File 'lib/edit_in_place/builder.rb', line 24 def initialize @config = EditInPlace.config.dup end |
Instance Attribute Details
#config ⇒ Configuration
This configuration is initially derived from the global configuration defined in EditInPlace.config.
The Configuration instance that stores the options for this EditInPlace::Builder.
21 22 23 |
# File 'lib/edit_in_place/builder.rb', line 21 def config @config end |
Instance Method Details
#configure {|config| ... } ⇒ void
This method returns an undefined value.
Configures this EditInPlace::Builder by yielding its configuration to the given block. For example,
@builder = EditInPlace::Builder.new
@builder.configure do |c|
c..mode = :editing
c..middlewares = [:one, :two]
end
Note that this method is simply a convenience method, and the above code is exactly equivalent to the following:
@builder = EditInPlace::Builder.new
@builder.config..mode = :editing
@builder.config..middlewares = [:one, :two]
55 56 57 |
# File 'lib/edit_in_place/builder.rb', line 55 def configure yield config if block_given? end |
#dup ⇒ Builder
Creates a deep copy of this EditInPlace::Builder, whose configuration can be safely modified.
30 31 32 33 34 |
# File 'lib/edit_in_place/builder.rb', line 30 def dup b = Builder.new b.config = config.dup b end |
#field(type, options, *args) ⇒ String #field(type, *args) ⇒ String
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/edit_in_place/builder.rb', line 77 def field(type, *args) (args) args[0] = config..merge(args[0]) definition = Middlegem::ArrayDefinition.new(config.defined_middlewares) stack = Middlegem::Stack.new(definition, middlewares: args[0].middlewares) args = stack.call(*args) type = evaluate_field_type(type) type.render(*args) end |
#scoped(field_options = {}) {|scoped_builder| ... } ⇒ string
Yields a new, scoped EditInPlace::Builder with the given field options. This method is helpful when many fields require the same options. One use, for example, is to easily give field types access to the current view context, like so:
<!-- some_view.html.erb -->
<%= @builder.scoped view: self do |b|
<%= b.field(:example_type, 'random scoped field') %>
<!-- ... -->
<% end %>
Now all fields generated using b will have access to self as the view context in FieldOptions#view.
109 110 111 112 113 114 115 116 |
# File 'lib/edit_in_place/builder.rb', line 109 def scoped( = {}) = FieldOptions.new() unless .is_a? FieldOptions scoped_builder = dup scoped_builder.config..merge!() yield scoped_builder end |