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_cache_setting_method

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, #prepare_stub_nest, #pretty_path, #render!, #show_debug_info?, #show_view?, #stub_render, #supports_view?, #view_cache_setting, #view_method, #view_method_name, #voo, #with_voo

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?, #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.



73
74
75
76
77
78
79
# File 'lib/card/format.rb', line 73

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.



66
67
68
# File 'lib/card/format.rb', line 66

def card
  @card
end

#error_statusObject

Returns the value of attribute error_status.



67
68
69
# File 'lib/card/format.rb', line 67

def error_status
  @error_status
end

#formObject

Returns the value of attribute form.



67
68
69
# File 'lib/card/format.rb', line 67

def form
  @form
end

#main_optsObject (readonly)

Returns the value of attribute main_opts.



66
67
68
# File 'lib/card/format.rb', line 66

def main_opts
  @main_opts
end

#parentObject (readonly)

Returns the value of attribute parent.



66
67
68
# File 'lib/card/format.rb', line 66

def parent
  @parent
end

Class Method Details

.view_caching?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/card/format.rb', line 69

def self.view_caching?
  true
end

Instance Method Details

#controllerObject



107
108
109
# File 'lib/card/format.rb', line 107

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

#include_set_format_modulesObject



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

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



128
129
130
# File 'lib/card/format.rb', line 128

def mime_type
  "text/text"
end

#page(controller, view, slot_opts) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/card/format.rb', line 95

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



103
104
105
# File 'lib/card/format.rb', line 103

def params
  Env.params
end

#require_card_to_initialize!Object

Raises:



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

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



111
112
113
# File 'lib/card/format.rb', line 111

def session
  Env.session
end

#tagged(view, tag) ⇒ Object



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

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

#templateObject



115
116
117
118
119
120
121
122
# File 'lib/card/format.rb', line 115

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



132
133
134
# File 'lib/card/format.rb', line 132

def to_sym
  Card::Format.format_sym self
end