Class: Adminable::Presenters::Entry

Inherits:
Base
  • Object
show all
Defined in:
lib/adminable/presenters/entry.rb

Instance Method Summary collapse

Constructor Details

#initialize(entry, field: nil) ⇒ Entry

Returns a new instance of Entry.



4
5
6
7
8
# File 'lib/adminable/presenters/entry.rb', line 4

def initialize(entry, field: nil)
  @entry = entry
  @field = field
  @value = entry.public_send(field.name) if field
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



78
79
80
# File 'lib/adminable/presenters/entry.rb', line 78

def method_missing(method_name, *args, &block)
  entry.public_send(method_name, *args, &block)
end

Instance Method Details

#has_one_valueObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/adminable/presenters/entry.rb', line 62

def has_one_value
  association = @entry.association(@field.name).klass

  if @value
    Adminable::Presenters::Entry(@value).link_to_self
  else
    view.link_to(
      I18n.t(
        'adminable.ui.no_has_one',
        resource: association.model_name.human
      ),
      polymorphic_path(association),
    )
  end
end


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/adminable/presenters/entry.rb', line 30

def link_to_delete
  view.link_to(
    I18n.t('adminable.buttons.delete'),
    polymorphic_path(@entry),
    class: 'btn btn-danger-outline pull-xs-right',
    method: :delete,
    data: {
      confirm: I18n.t('adminable.ui.confirm')
    }
  )
end


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/adminable/presenters/entry.rb', line 50

def link_to_delete_small
  view.link_to(
    I18n.t('adminable.buttons.delete'),
    polymorphic_path(entry),
    class: 'label label-danger',
    method: :delete,
    data: {
      confirm: I18n.t('adminable.ui.confirm')
    }
  )
end


42
43
44
45
46
47
48
# File 'lib/adminable/presenters/entry.rb', line 42

def link_to_edit_small
  view.link_to(
    I18n.t('adminable.buttons.edit'),
    edit_polymorphic_path(entry),
    class: 'label label-primary'
  )
end


20
21
22
23
24
25
26
27
28
# File 'lib/adminable/presenters/entry.rb', line 20

def link_to_self
  return to_name unless resource

  view.link_to(
    to_name,
    edit_polymorphic_path(entry),
    target: '_blank'
  )
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/adminable/presenters/entry.rb', line 82

def respond_to_missing?(method_name, *)
  entry.respond_to?(method_name)
end

#to_nameObject



10
11
12
13
14
15
16
17
18
# File 'lib/adminable/presenters/entry.rb', line 10

def to_name
  %i(name title email login id).each do |method_name|
    begin
      return entry.public_send(method_name)
    rescue NoMethodError
      next
    end
  end
end