Module: Arara::BaseComponent

Defined Under Namespace

Classes: MissingTag

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/components/arara/base_component.rb', line 5

def self.included(mod)
  attr_reader :user_content, :user_variant, :user_tag, :user_data, :id, :options

  def initialize(tag: nil, content: nil, variant: nil, id: nil, data: nil, **options)
    @user_tag = tag.try(:to_s) || default_html_tag
    @user_variant = variant.try(:to_s)
    @user_content = content
    @user_data = data || {}
    @html_class = options.fetch(:class, "")
    @options = options
    @id = id
  end

  def html_content
    return content if content
    user_content
  end

  def html_class
    classes = default_html_class ? [default_html_class] : []
    classes.push(@html_class) unless @html_class.try(:empty?)
    classes.empty? ? [] : classes
  end

  def html_tag
    raise MissingTag.new('You need to specify a tag') if @user_tag.nil?
    @user_tag
  end

  def html_options
    opts = options.dup
    opts[:class] = html_class unless html_class.empty?
    opts[:data] = html_data unless html_data.empty?
    opts[:id] = id if id
    opts
  end

  def html_data
    data_controller = default_data_controller ? [default_data_controller] : []
    data_controller.push(user_data[:controller]) if user_data.has_key?(:controller)
    data_controller = (data_controller.empty? ? nil : data_controller.join(" "))

    data = user_data.dup
    data.merge!({controller: data_controller})
    data
  end

  def default_data_controller
  end

  def default_html_class
  end

  def default_html_tag
  end
end

Instance Method Details

#default_data_controllerObject



52
53
# File 'app/components/arara/base_component.rb', line 52

def default_data_controller
end

#default_html_classObject



55
56
# File 'app/components/arara/base_component.rb', line 55

def default_html_class
end

#default_html_tagObject



58
59
# File 'app/components/arara/base_component.rb', line 58

def default_html_tag
end

#html_classObject



23
24
25
26
27
# File 'app/components/arara/base_component.rb', line 23

def html_class
  classes = default_html_class ? [default_html_class] : []
  classes.push(@html_class) unless @html_class.try(:empty?)
  classes.empty? ? [] : classes
end

#html_contentObject



18
19
20
21
# File 'app/components/arara/base_component.rb', line 18

def html_content
  return content if content
  user_content
end

#html_dataObject



42
43
44
45
46
47
48
49
50
# File 'app/components/arara/base_component.rb', line 42

def html_data
  data_controller = default_data_controller ? [default_data_controller] : []
  data_controller.push(user_data[:controller]) if user_data.has_key?(:controller)
  data_controller = (data_controller.empty? ? nil : data_controller.join(" "))
    
  data = user_data.dup
  data.merge!({controller: data_controller})
  data
end

#html_optionsObject



34
35
36
37
38
39
40
# File 'app/components/arara/base_component.rb', line 34

def html_options
  opts = options.dup
  opts[:class] = html_class unless html_class.empty?
  opts[:data] = html_data unless html_data.empty?
  opts[:id] = id if id
  opts
end

#html_tagObject

Raises:



29
30
31
32
# File 'app/components/arara/base_component.rb', line 29

def html_tag
  raise MissingTag.new('You need to specify a tag') if @user_tag.nil?
  @user_tag
end

#initialize(tag: nil, content: nil, variant: nil, id: nil, data: nil, **options) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'app/components/arara/base_component.rb', line 8

def initialize(tag: nil, content: nil, variant: nil, id: nil, data: nil, **options)
  @user_tag = tag.try(:to_s) || default_html_tag
  @user_variant = variant.try(:to_s)
  @user_content = content
  @user_data = data || {}
  @html_class = options.fetch(:class, "")
  @options = options
  @id = id
end