Class: Super::Display

Inherits:
Object
  • Object
show all
Includes:
Schema::Common
Defined in:
lib/super/display.rb,
lib/super/display/guesser.rb,
lib/super/display/schema_types.rb

Overview

This schema type is meant to be used for +#index+ or +#show+ actions to transform database fields into something that is human friendly.

class MembersController::Controls
  # ...

  def show_schema
    Super::Display.new do |fields, type|
      fields[:name] = type.manual { |name| name }
      fields[:rank] = type.manual { |rank| rank }
      fields[:position] = type.manual { |position| position }
      fields[:ship] = type.manual { |ship| "#{ship.name} (Ship ##{ship.id})" }
      fields[:created_at] = type.manual { |created_at| created_at.iso8601 }
      fields[:updated_at] = type.manual { |updated_at| updated_at.iso8601 }
    end
  end

  # ...
end

Defined Under Namespace

Classes: Guesser, SchemaTypes

Instance Method Summary collapse

Methods included from Schema::Common

#each_attribute, #each_attribute_name

Constructor Details

#initialize {|@fields, @schema_types| ... } ⇒ Display

Returns a new instance of Display.

Yields:

  • (@fields, @schema_types)


28
29
30
31
32
33
# File 'lib/super/display.rb', line 28

def initialize
  @fields = Super::Schema::Fields.new
  @schema_types = SchemaTypes.new(fields: @fields)

  yield(@fields, @schema_types)
end

Instance Method Details

#apply(action:) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/super/display.rb', line 35

def apply(action:)
  @action_inquirer = action
  return self if !@action_inquirer.index?
  return self if @schema_types.actions_called?
  @fields[:actions] = @schema_types.actions
  self
end

#render_attribute(template:, record:, column:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/super/display.rb', line 57

def render_attribute(template:, record:, column:)
  formatter = @fields[column]
  formatter = formatter.build if formatter.respond_to?(:build)

  formatted =
    SchemaTypes::TYPES
    .case(formatter.type)
    .when(:record) do
      formatter.present(column, record)
    end
    .when(:column) do
      value = record.public_send(column)
      formatter.present(column, value)
    end
    .when(:none) do
      formatter.present(column)
    end
    .result

  if formatted.respond_to?(:to_partial_path)
    if formatted.respond_to?(:locals)
      formatted.locals[:record] ||= record
      template.render(formatted, formatted.locals)
    else
      template.render(formatted)
    end
  else
    formatted
  end
end

#to_partial_pathObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/super/display.rb', line 43

def to_partial_path
  if @action_inquirer.nil?
    raise Super::Error::Initalization,
      "You must call the `#apply` method after instantiating Super::Display"
  elsif @action_inquirer.index?
    "display_index"
  elsif @action_inquirer.show?
    "display_show"
  else
    "display_#{@action_inquirer.action}"
  end
end