Class: EditInPlace::Builder

Inherits:
Object
  • Object
show all
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.

Author:

  • Jacob Lockard

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Creates a new instance of EditInPlace::Builder.

Since:

  • 0.1.0


24
25
26
# File 'lib/edit_in_place/builder.rb', line 24

def initialize
  @config = EditInPlace.config.dup
end

Instance Attribute Details

#configConfiguration

Note:

This configuration is initially derived from the global configuration defined in EditInPlace.config.

The Configuration instance that stores the options for this EditInPlace::Builder.

Returns:

Since:

  • 0.1.0


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.field_options.mode = :editing
  c.field_options.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.field_options.mode = :editing
@builder.config.field_options.middlewares = [:one, :two]

Yield Parameters:

Yield Returns:

  • (void)

See Also:

Since:

  • 0.1.0


55
56
57
# File 'lib/edit_in_place/builder.rb', line 55

def configure
  yield config if block_given?
end

#dupBuilder

Creates a deep copy of this EditInPlace::Builder, whose configuration can be safely modified.

Returns:

Since:

  • 0.1.0


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

#evaluate_field_type(type) ⇒ FieldType (private)

Gets an appropriate FieldType instance for the given raw field type argument. In particular:

  • When the input is already a FieldType instance, that instance is simply returned.

  • When the input is a symbol, attempts to find a registered field type associated with it.

  • Otherwise, raises an error.

Returns:

Since:

  • 0.1.0


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/edit_in_place/builder.rb', line 147

def evaluate_field_type(type)
  case type
  when FieldType
    type
  when Symbol
    result = config.field_types.find(type)
    raise UnregisteredFieldTypeError, type if result.nil?

    result
  else
    raise InvalidFieldTypeError, type
  end
end

#field(type, options, *args) ⇒ String #field(type, *args) ⇒ String

Overloads:

  • #field(type, options, *args) ⇒ String

    Renders a single field of the given type with the given field option and arguments to be passed to the field type renderer.

    Parameters:

    • type (FieldType, Symbol)

      the type of field to render, either an actual instance of FieldType, or the symbol name of a registered field type.

    • options (FieldOptions, Hash)

      the field options to be used when rendering the field. These options are defined in FieldOptions. Either an actual instance of FieldOptions or a hash are acceptable.

    • args (Array)

      the arguments to be passed to the field renderer.

    Returns:

    • (String)

      the rendered field, as HTML.

  • #field(type, *args) ⇒ String

    Renders a single field of the given type with the given arguments to be passed to the field type renderer. The default field options (as defined in FieldOptions) will be used.

    Parameters:

    • type (FieldType, Symbol)

      the type of field to render, either an actual instance of FieldType, or the symbol name of a registered field type.

    • args (Array)

      the arguments to be passed to the field renderer.

    Returns:

    • (String)

      the rendered field, as HTML.

Since:

  • 0.1.0


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/edit_in_place/builder.rb', line 77

def field(type, *args)
  inject_field_options!(args)
  args[0] = config.field_options.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

#inject_field_options!(args) ⇒ void (private)

This method returns an undefined value.

Ensures that the first argument in the given list of arguments is a valid, appropriate FieldOptions instance for the list of arguments. In particular:

  • When the first argument is already an instance of FieldOptions, the argument list is not touched.

  • When the first argument is a hash, then it is converted to a FieldOptions instance.

  • Otherwise, the default FieldOptions instance is prepended to the argument list.

Parameters:

  • args (Array)

    the raw arguments into which to inject the field options.

Since:

  • 0.1.0


129
130
131
132
133
134
135
136
137
138
139
# File 'lib/edit_in_place/builder.rb', line 129

def inject_field_options!(args)
  options = args.first

  return if options.is_a? FieldOptions

  if options.is_a? Hash
    args[0] = FieldOptions.new(options)
  else
    args.unshift(FieldOptions.new)
  end
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.

Parameters:

  • field_options (FieldOptions, Hash) (defaults to: {})

    the field options that the scoped builder should have. Note that these options will be merged (using FieldOptions#merge!) with the current ones. Either an actual FieldOptions instance or a hash of options are acceptable.

Yield Parameters:

Yield Returns:

  • (string)

    the output generated with the scoped builder.

Returns:

  • (string)

    the output of the block.

Since:

  • 0.1.0


109
110
111
112
113
114
115
116
# File 'lib/edit_in_place/builder.rb', line 109

def scoped(field_options = {})
  field_options = FieldOptions.new(field_options) unless field_options.is_a? FieldOptions

  scoped_builder = dup
  scoped_builder.config.field_options.merge!(field_options)

  yield scoped_builder
end