Module: AnimateIt::ViewHelpers

Included in:
Scene
Defined in:
lib/animate_it/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#absolute_fill(class_name: nil, style: nil, **attributes, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/animate_it/view_helpers.rb', line 7

def absolute_fill(class_name: nil, style: nil, **attributes, &block)
  content = block.call

  tag.div(
    **attributes,
    class: ["animate-it-absolute-fill", class_name].compact,
    style: absolute_fill_style(style)
  ) do
    content
  end
end

#build_factory(name, *traits, **attributes) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/animate_it/view_helpers.rb', line 39

def build_factory(name, *traits, **attributes)
  raise AnimateIt::Error, "FactoryBot is not available" unless defined?(FactoryBot)

  FactoryBot.build(name, *traits, **attributes)
end

#build_stubbed(name, *traits, **attributes) ⇒ Object

Raises:



45
46
47
48
49
# File 'lib/animate_it/view_helpers.rb', line 45

def build_stubbed(name, *traits, **attributes)
  raise AnimateIt::Error, "FactoryBot is not available" unless defined?(FactoryBot)

  FactoryBot.build_stubbed(name, *traits, **attributes)
end

#motion_asset(path) ⇒ Object



31
32
33
# File 'lib/animate_it/view_helpers.rb', line 31

def motion_asset(path)
  view_context.asset_path(path)
end

#motion_image(path, **attributes) ⇒ Object



35
36
37
# File 'lib/animate_it/view_helpers.rb', line 35

def motion_image(path, **attributes)
  tag.img(**attributes, src: motion_asset(path))
end

#render_partial(partial, locals: {}) ⇒ Object



27
28
29
# File 'lib/animate_it/view_helpers.rb', line 27

def render_partial(partial, locals: {}, **)
  view_context.render(partial:, locals:, **)
end

#render_scene_template(name, assigns: {}) ⇒ Object



23
24
25
# File 'lib/animate_it/view_helpers.rb', line 23

def render_scene_template(name, assigns: {}, **)
  render_template("#{sidecar_template_root}/#{name}", assigns:, **)
end

#render_template(template, assigns: {}) ⇒ Object



19
20
21
# File 'lib/animate_it/view_helpers.rb', line 19

def render_template(template, assigns: {}, **)
  view_context.render(template:, assigns:, layout: false, **)
end

#stub_methods(object, **stubs) ⇒ Object

Define singleton methods on object from a => value hash. Non-callable values are auto-wrapped in a lambda that ignores its args, so stub_methods(match, finished_state?: false) works the same way as stub_methods(match, finished_state?: -> { false }). The wrapping lambda accepts *args so arity-bearing methods like user_has_reviewed?(user) don't raise ArgumentError on the value-shortcut form.



58
59
60
61
62
63
64
# File 'lib/animate_it/view_helpers.rb', line 58

def stub_methods(object, **stubs)
  stubs.each do |name, value|
    body = value.respond_to?(:call) ? value : ->(*) { value }
    object.define_singleton_method(name) { |*args| body.call(*args) }
  end
  object
end

#style(*rules, **properties) ⇒ Object



3
4
5
# File 'lib/animate_it/view_helpers.rb', line 3

def style(*rules, **properties)
  Style.build(*rules, **properties)
end