lib/nice_partials/partial.rb in nice_partials-0.9.1 vs lib/nice_partials/partial.rb in nice_partials-0.9.2
- old
+ new
@@ -2,12 +2,10 @@
class Partial
autoload :Content, "nice_partials/partial/content"
autoload :Section, "nice_partials/partial/section"
autoload :Stack, "nice_partials/partial/stack"
- delegate_missing_to :@view_context
-
def initialize(view_context, local_assigns = nil)
@view_context, @local_assigns = view_context, local_assigns
end
def yield(*arguments, &block)
@@ -17,11 +15,11 @@
content_for(*arguments, &block)
end
end
def helpers(&block)
- self.class.class_eval(&block)
+ Helpers.class_eval(&block)
end
# `translate` is a shorthand to set `content_for` with content that's run through
# the view's `translate`/`t` context.
#
@@ -75,11 +73,11 @@
@sections&.dig(name).present?
end
alias content_for? section?
def content_for(...)
- section(...)&.to_s
+ section(...).presence&.to_s
end
def slice(*keys)
keys.index_with { content_for _1 }
end
@@ -92,20 +90,31 @@
def section_from(name)
@sections ||= {} and @sections[name] ||= Section.new(@view_context, @local_assigns&.dig(name))
end
+ def respond_to_missing?(meth, include_private = false)
+ meth != :to_ary # Avoid creating a section when doing `*partial`.
+ end
+
def method_missing(meth, *arguments, **keywords, &block)
- if @view_context.respond_to?(meth)
- @view_context.public_send(meth, *arguments, **keywords, &block)
- else
- define_accessor meth and public_send(meth, *arguments, **keywords, &block)
- end
+ define_accessor meth and public_send(meth, *arguments, **keywords, &block)
end
def define_accessor(name)
name = name.to_s.chomp("?").to_sym
self.class.define_method(name) { |content = nil, **options, &block| section(name, content, **options, &block) }
self.class.define_method("#{name}?") { section?(name) }
+ end
+
+ def helpers_context
+ @helpers_context ||= Helpers.new(@view_context)
+ end
+
+ class Helpers < SimpleDelegator
+ def self.method_added(name)
+ super
+ NicePartials::Partial.delegate name, to: :helpers_context
+ end
end
end
end