Class: Dunlop::StateBadge

Inherits:
Object
  • Object
show all
Defined in:
app/services/dunlop/state_badge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, target, options = {}) ⇒ StateBadge

Returns a new instance of StateBadge.



3
4
5
6
7
8
9
# File 'app/services/dunlop/state_badge.rb', line 3

def initialize(context, target, options = {})
  @context = context
  @target = target
  @target = target.object if target && target.respond_to?(:decorated?) && target.decorated?
  @options = options
  @state = options[:state] || target.try(:state) # allow hard setting of state, take current state otherwise
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



2
3
4
# File 'app/services/dunlop/state_badge.rb', line 2

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'app/services/dunlop/state_badge.rb', line 2

def options
  @options
end

#stateObject (readonly)

Returns the value of attribute state.



2
3
4
# File 'app/services/dunlop/state_badge.rb', line 2

def state
  @state
end

#targetObject (readonly)

Returns the value of attribute target.



2
3
4
# File 'app/services/dunlop/state_badge.rb', line 2

def target
  @target
end

Instance Method Details

#badge_dataObject



29
30
31
32
33
34
# File 'app/services/dunlop/state_badge.rb', line 29

def badge_data
  result = (options[:data] || {})
  result = result.merge(id: target.id, resource: underscored, state: state)
  result.merge!(target: link_to) if link_to.present?
  result
end

#htmlObject

Generate html as string in stead of a partial. This made the workflow_instances index page render a factor 4 faster



37
38
39
40
41
42
43
# File 'app/services/dunlop/state_badge.rb', line 37

def html
  #content = options[:link_to] ? context.link_to(inner_badge_html, options[:link_to]) : inner_badge_html
  content = inner_badge_html
  context. :span, content,
    class: "display-badge #{underscored.gsub(/\W/, ' ')} #{state}",
    data: badge_data
end

#human_state_nameObject

Code climate extraction



24
25
26
27
# File 'app/services/dunlop/state_badge.rb', line 24

def human_state_name
  state_name = target.class.human_state_name(state)
  state_name
end

#inner_badge_htmlObject



45
46
47
48
# File 'app/services/dunlop/state_badge.rb', line 45

def inner_badge_html
  badge_name = context.t("display_badge", scope: underscored, default: Proc.new{ I18n.t('display_badge', scope: underscored.split('/').first, default: target.class.model_name.human) })
  %|<span class="pre-display"></span><span class="badge-name">#{badge_name}</span><span class="human-state">#{human_state_name}</span><span class="post-display">#{options[:state_append_text]}</span>|.html_safe
end


11
12
13
# File 'app/services/dunlop/state_badge.rb', line 11

def link_to
  options[:link_to]
end

#underscoredObject



19
20
21
# File 'app/services/dunlop/state_badge.rb', line 19

def underscored
  @underscored = target.class.name.underscore
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/services/dunlop/state_badge.rb', line 15

def valid?
  target.present? && state.present?
end