Class: Card::Format

Inherits:
Object
  • Object
show all
Extended by:
Registration
Includes:
Env::Location, Content, ContextNames, Error, MethodDelegation, Nesting, Permission, Render
Defined in:
lib/card/format.rb,
lib/card/format/nest.rb,
lib/card/format/error.rb,
lib/card/format/render.rb,
lib/card/format/content.rb,
lib/card/format/nesting.rb,
lib/card/format/nest/fetch.rb,
lib/card/format/permission.rb,
lib/card/format/nesting/main.rb,
lib/card/format/nesting/mode.rb,
lib/card/format/registration.rb,
lib/card/format/context_names.rb,
lib/card/format/method_delegation.rb,
lib/card/format/nesting/subformat.rb,
mod/core/format/data_format.rb,
mod/core/format/html_format.rb,
mod/core/format/text_format.rb,
mod/basic_formats/format/js_format.rb,
mod/email/format/email_html_format.rb,
mod/email/format/email_text_format.rb,
mod/basic_formats/format/css_format.rb,
mod/basic_formats/format/csv_format.rb,
mod/basic_formats/format/rss_format.rb,
mod/basic_formats/format/xml_format.rb,
mod/basic_formats/format/file_format.rb,
mod/basic_formats/format/json_format.rb

Overview

Views are the primary way users interact with cards. Card::Format and its subclasses (HtmlFormat, JsonFormat, XmlFormat, etc) are responsible for defining and rendering views.

However, Deck-coders (those who code in the Card/Decko framework) rarely write code directly in these classes. Instead they organize their code using mods. The Mod docs explain how to set up a mod. Once you've done that, you're ready to define a view. These docs will introduce the basics of view definition and

Here is a very simple view that just defines a label for the card – its name:

view :label do
  card.name
end

If a format is not specified, the view is defined on the base format class, Card::Format. The following two definitions are equivalent to the definition above:

format do
  view(:label) { card.name }
end

format(:base) { view(:label) { card.name } }

But suppose you would like this view to appear differently in different output formats. For example, you'd like this label to have a tag with a class attribute HTML so that you can style it with CSS.

format :html do
  view :label do
    div(class: "my-label") { card.name }
  end
end

Note that in place of card.name, you could also use super, because this view is translated into a method on Card::Format::HtmlFormat, which inherits from Card::Format.

Common arguments for view definitions

  • :perms - restricts view permissions. Value can be :create, :read, :update, :delete, or a Proc.
  • :tags - tag view as needed.

The most commmon tag is "unknown_ok," which indicates that a view can be rendered even if the card is "unknown" (not real or virtual).

Rendering views

To render our label view, you can use either of these:

render :label
render_label

Defined Under Namespace

Modules: Content, ContextNames, Error, MethodDelegation, Nesting, Permission, Registration, Render Classes: CssFormat, CsvFormat, DataFormat, EmailHtmlFormat, EmailTextFormat, FileFormat, HtmlFormat, JsFormat, JsonFormat, Nest, RssFormat, TextFormat, XmlFormat

Constant Summary collapse

VIEW_VARS =
%i[perms denial closed].freeze

Constants included from MethodDelegation

MethodDelegation::RENDER_METHOD_RE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Registration

class_from_name, extract_class_vars, extract_view_tags, format_ancestry, format_class, format_class_name, format_sym, interpret_view_opts, new, register, view_setting_method_name

Methods included from MethodDelegation

#api_render, #method_missing, #pass_method_to_template_object, #render_args, #respond_to_missing?

Methods included from Error

#error_cardname, #loud_error, #loud_error?, #quiet_error, #rendering_error, #rescue_view

Methods included from Content

#add_class, #content_nest, #css_classes, #format_date, #id_counter, #output, #prepend_class, #process_content, #unique_id

Methods included from ContextNames

#add_name_context, #context_names, #context_names_from_params, #context_names_to_params, #initial_context_names, #relevant_context_names, #title_in_context

Methods included from Render

#add_debug_info, #before_view, #current_view, #expand_stubs, #final_render, #final_render_call, #prepare_stub_nest, #pretty_path, #raise_wrap_error, #render!, #render_with_card_layout, #render_with_wrapper, #show_debug_info?, #show_view?, #stub_render, #supports_view?, #view_cache_setting, #view_method, #view_setting, #voo, #with_voo, #with_wrapper, #wrap_with_wrapper

Methods included from Permission

#check_view, #deny_view, #handle_view_denial, #ok?, #ok_view, #permitted_view, #subformats_nested_too_deeply?, #task_denied_for_view, #unknown_disqualifies_view?, #view_always_permitted?, #view_for_unknown

Methods included from Nesting

#default_nest_view, #field_nest, #implicit_nest_view, #nest, #nest_path

Methods included from Nesting::Mode

#configured_view_in_closed_mode, #hide_view_in_edit_mode?, #modal_nest_view, #nest_mode, #view_in_closed_mode, #view_in_edit_mode, #with_altered_nest_mode, #with_nest_mode

Methods included from Nesting::Subformat

#depth, #field_subformat, #focal?, #inherit, #main, #main?, #root, #root?, #subformat

Methods included from Nesting::Main

#already_mained?, #main!, #main_nest, #main_nest?, #main_nest_options, #wrap_main

Methods included from Env::Location

#card_path, #card_url, #protocol_and_host

Constructor Details

#initialize(card, opts = {}) ⇒ Format

Returns a new instance of Format.



85
86
87
88
89
90
91
# File 'lib/card/format.rb', line 85

def initialize card, opts={}
  @card = card
  require_card_to_initialize!
  opts.each { |key, value| instance_variable_set "@#{key}", value }
  include_set_format_modules
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Card::Format::MethodDelegation

Instance Attribute Details

#cardObject (readonly)

Returns the value of attribute card.



78
79
80
# File 'lib/card/format.rb', line 78

def card
  @card
end

#error_statusObject

Returns the value of attribute error_status.



79
80
81
# File 'lib/card/format.rb', line 79

def error_status
  @error_status
end

#formObject

Returns the value of attribute form.



79
80
81
# File 'lib/card/format.rb', line 79

def form
  @form
end

#main_optsObject (readonly)

Returns the value of attribute main_opts.



78
79
80
# File 'lib/card/format.rb', line 78

def main_opts
  @main_opts
end

Returns the value of attribute modal_opts.



78
79
80
# File 'lib/card/format.rb', line 78

def modal_opts
  @modal_opts
end

#parentObject (readonly)

Returns the value of attribute parent.



78
79
80
# File 'lib/card/format.rb', line 78

def parent
  @parent
end

#renderedObject

Returns the value of attribute rendered.



79
80
81
# File 'lib/card/format.rb', line 79

def rendered
  @rendered
end

Class Method Details

.view_caching?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/card/format.rb', line 81

def self.view_caching?
  true
end

Instance Method Details

#controllerObject



120
121
122
# File 'lib/card/format.rb', line 120

def controller
  @controller || Env[:controller] ||= CardController.new
end

#include_set_format_modulesObject



100
101
102
103
104
105
106
# File 'lib/card/format.rb', line 100

def include_set_format_modules
  self.class.format_ancestry.reverse_each do |klass|
    card.set_format_modules(klass).each do |m|
      singleton_class.send :include, m
    end
  end
end

#mime_typeObject



141
142
143
# File 'lib/card/format.rb', line 141

def mime_type
  "text/plain"
end

#page(controller, view, slot_opts) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/card/format.rb', line 108

def page controller, view, slot_opts
  @controller = controller
  context_names # loads names and removes #name_context from slot_opts
  @card.run_callbacks :show_page do
    show view, slot_opts
  end
end

#paramsObject



116
117
118
# File 'lib/card/format.rb', line 116

def params
  Env.params
end

#require_card_to_initialize!Object

Raises:



93
94
95
96
97
98
# File 'lib/card/format.rb', line 93

def require_card_to_initialize!
  return if @card

  msg = I18n.t :exception_init_without_card, scope: "lib.card.format"
  raise Card::Error, msg
end

#sessionObject



124
125
126
# File 'lib/card/format.rb', line 124

def session
  Env.session
end

#tagged(view, tag) ⇒ Object



137
138
139
# File 'lib/card/format.rb', line 137

def tagged view, tag
  self.class.tagged view, tag
end

#templateObject



128
129
130
131
132
133
134
135
# File 'lib/card/format.rb', line 128

def template
  @template ||= begin
    c = controller
    t = ActionView::Base.new c.class.view_paths, { _routes: c._routes }, c
    t.extend c.class._helpers
    t
  end
end

#to_symObject



145
146
147
# File 'lib/card/format.rb', line 145

def to_sym
  Card::Format.format_sym self
end