Class: Storefront::Components::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers::ContentHelper
Defined in:
lib/storefront/components/base.rb

Constant Summary

Constants included from Helpers::ContentHelper

Helpers::ContentHelper::SCOPES, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL, Helpers::ContentHelper::SCOPES_WITH_NESTED_MODEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ContentHelper

#encoded_contents, #font_face_data_uri, #html5_time, #read_binary_file, #read_image_size, #rendered_text, #sanitize, #t?

Constructor Details

#initialize(template, *args) ⇒ Base

Returns a new instance of Base.



14
15
16
17
# File 'lib/storefront/components/base.rb', line 14

def initialize(template, *args)
  @template = template
  @options  = extract_options!(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/storefront/components/base.rb', line 85

def method_missing(method, *args, &block)
  if view_context.respond_to?(method)
    self.class.class_eval %{
      def #{method}(*args, &block)
        view_context.#{method}(*args, &block)
      end
    }
    send method, *args, &block
  else
    super
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/storefront/components/base.rb', line 12

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



12
13
14
# File 'lib/storefront/components/base.rb', line 12

def template
  @template
end

Instance Method Details

#component_nameObject



65
66
67
# File 'lib/storefront/components/base.rb', line 65

def component_name
  @component_name ||= self.class.name.split("::").last.underscore
end

#extract_classes!(type) ⇒ Object



69
70
71
72
73
74
# File 'lib/storefront/components/base.rb', line 69

def extract_classes!(type)
  [
    storefront_config.widget_class,
    type.to_s.strip.gsub(/[\s|_]+/, storefront_config.separator).squeeze(storefront_config.separator)
  ]
end

#extract_options!(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/storefront/components/base.rb', line 47

def extract_options!(*args)
  options       = args.extract_options!
  key           = args.shift
  
  options[:title] ||= false
  options[:key] ||= key
  options[:outer_html] ||= {}
  
  options[:body_html] ||= {}
  options[:footer_html] ||= {}
  
  unless options[:widget_class] == false
    merge_class! options[:outer_html], *extract_classes!(component_name)
  end
  
  options
end

#inside?(key) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/storefront/components/base.rb', line 34

def inside?(key)
  index = pointer.index(key)
  !!(index && index >= 0 && index < pointer.length - 1)
end

#pointerObject



39
40
41
# File 'lib/storefront/components/base.rb', line 39

def pointer
  Storefront::Components.pointer
end

#render(&block) ⇒ Object

The to_s actually renders the object to HAML.

This allows you to pass the widget into a variable the same way you would actually render it in HAML.



23
24
25
# File 'lib/storefront/components/base.rb', line 23

def render(&block)
  
end

#render_with_pointer(&block) ⇒ Object



27
28
29
30
31
32
# File 'lib/storefront/components/base.rb', line 27

def render_with_pointer(&block)
  pointer.push(component_name)
  result = render(&block)  
  pointer.pop
  result
end

#to_sObject



43
44
45
# File 'lib/storefront/components/base.rb', line 43

def to_s
  render
end