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, vars: nil, **attributes, &block) ⇒ Object



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

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

  if vars
    style = [style.presence, animation_vars(vars)].compact.join("; ")
    attributes = { data: { animate_vars: vars } }.deep_merge(attributes)
  end

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

#animate_text(key, tag_name: :span, **attributes) ⇒ Object



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

def animate_text(key, tag_name: :span, **attributes)
  attributes = { data: { animate_text: key } }.deep_merge(attributes)
  tag.public_send(tag_name, evaluate_text_track(key).to_s, **attributes)
end

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

Raises:



49
50
51
52
53
# File 'lib/animate_it/view_helpers.rb', line 49

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:



55
56
57
58
59
# File 'lib/animate_it/view_helpers.rb', line 55

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



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

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

#motion_image(path, **attributes) ⇒ Object



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

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

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



37
38
39
# File 'lib/animate_it/view_helpers.rb', line 37

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

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



33
34
35
# File 'lib/animate_it/view_helpers.rb', line 33

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

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



29
30
31
# File 'lib/animate_it/view_helpers.rb', line 29

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.



68
69
70
71
72
73
74
# File 'lib/animate_it/view_helpers.rb', line 68

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