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
34
35
# File 'lib/super/display.rb', line 28

def initialize
  @fields = Super::Schema::Fields.new(
    transform_value_on_set: -> (val) { if val.respond_to?(:build) then val.build else val end }
  )
  @schema_types = SchemaTypes.new(fields: @fields)

  yield(@fields, @schema_types)
end

Instance Method Details

#apply(action:, format:) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/super/display.rb', line 37

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

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



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
87
88
# File 'lib/super/display.rb', line 60

def render_attribute(template:, record:, column:)
  formatter = @fields[column]

  formatted =
    SchemaTypes::TYPES
    .case(formatter.type)
    .when(:record) do
      formatter.present(column, record)
    end
    .when(:attribute) 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



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/super/display.rb', line 46

def to_partial_path
  if @action_inquirer.nil?
    raise Super::Error::Initialization,
      "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