Class: Dunlop::StateBadge
- Inherits:
-
Object
- Object
- Dunlop::StateBadge
- Defined in:
- app/services/dunlop/state_badge.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #badge_data ⇒ Object
-
#html ⇒ Object
Generate html as string in stead of a partial.
-
#human_state_name ⇒ Object
Code climate extraction.
-
#initialize(context, target, options = {}) ⇒ StateBadge
constructor
A new instance of StateBadge.
- #inner_badge_html ⇒ Object
- #link_to ⇒ Object
- #underscored ⇒ Object
- #valid? ⇒ Boolean
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, = {}) @context = context @target = target @target = target.object if target && target.respond_to?(:decorated?) && target.decorated? @options = @state = [:state] || target.try(:state) # allow hard setting of state, take current state otherwise end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
2 3 4 |
# File 'app/services/dunlop/state_badge.rb', line 2 def context @context end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'app/services/dunlop/state_badge.rb', line 2 def @options end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
2 3 4 |
# File 'app/services/dunlop/state_badge.rb', line 2 def state @state end |
#target ⇒ Object (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_data ⇒ Object
29 30 31 32 33 34 |
# File 'app/services/dunlop/state_badge.rb', line 29 def badge_data result = ([:data] || {}) result = result.merge(id: target.id, resource: underscored, state: state) result.merge!(target: link_to) if link_to.present? result end |
#html ⇒ Object
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.content_tag :span, content, class: "display-badge #{underscored.gsub(/\W/, ' ')} #{state}", data: badge_data end |
#human_state_name ⇒ Object
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_html ⇒ Object
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">#{[:state_append_text]}</span>|.html_safe end |
#link_to ⇒ Object
11 12 13 |
# File 'app/services/dunlop/state_badge.rb', line 11 def link_to [:link_to] end |
#underscored ⇒ Object
19 20 21 |
# File 'app/services/dunlop/state_badge.rb', line 19 def underscored @underscored = target.class.name.underscore end |
#valid? ⇒ Boolean
15 16 17 |
# File 'app/services/dunlop/state_badge.rb', line 15 def valid? target.present? && state.present? end |