Module: AnimateIt::ViewHelpers
- Included in:
- Scene
- Defined in:
- lib/animate_it/view_helpers.rb
Instance Method Summary collapse
- #absolute_fill(class_name: nil, style: nil, vars: nil, **attributes, &block) ⇒ Object
- #animate_text(key, tag_name: :span, **attributes) ⇒ Object
- #build_factory(name, *traits, **attributes) ⇒ Object
- #build_stubbed(name, *traits, **attributes) ⇒ Object
- #motion_asset(path) ⇒ Object
- #motion_image(path, **attributes) ⇒ Object
- #render_partial(partial, locals: {}) ⇒ Object
- #render_scene_template(name, assigns: {}) ⇒ Object
- #render_template(template, assigns: {}) ⇒ Object
-
#stub_methods(object, **stubs) ⇒ Object
Define singleton methods on
objectfrom a => value hash. - #style(*rules, **properties) ⇒ Object
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
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
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 |