Class: Card::Format

Inherits:
Object
  • Object
show all
Extended by:
Registration
Includes:
Env::Location, Content, ContextNames, Error, MethodDelegation, Nest, 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/nest/main.rb,
lib/card/format/nest/mode.rb,
lib/card/format/nest/fetch.rb,
lib/card/format/permission.rb,
lib/card/format/registration.rb,
lib/card/format/context_names.rb,
lib/card/format/nest/subformat.rb,
lib/card/format/method_delegation.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/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,
mod/notifications/format/email_html_format.rb,
mod/notifications/format/email_text_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, Nest, Permission, Registration, Render Classes: CssFormat, CsvFormat, DataFormat, EmailHtmlFormat, EmailTextFormat, FileFormat, HtmlFormat, JsFormat, JsonFormat, RssFormat, TextFormat, XmlFormat

Constant Summary collapse

VIEW_VARS =
[ :perms, :denial, :closed, :error_code ]

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

#debug_error, #error_cardname, #rendering_error, #rescue_view

Methods included from Content

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

Methods included from ContextNames

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

Methods included from Render

#add_debug_info, #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, #view_options_with_defaults, #voo, #with_voo

Methods included from Permission

#assign_view_error_status, #check_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 Nest

#content_nest, #content_view?, #default_nest_view, #field_nest, #implicit_nest_view, #interpret_nest_options, #nest, #nest_path, #nest_recursion_risk?, #nest_render, #nest_subformat, #reuse_format?

Methods included from Nest::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 Nest::Subformat

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

Methods included from Nest::Main

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

Methods included from Nest::Fetch

#fetch_nested_card

Methods included from Env::Location

#card_path, #card_url, #page_path

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



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

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



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

def mime_type
  "text/text"
end

#page(controller, view, slot_opts) ⇒ Object



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

def page controller, view, slot_opts
  @controller = controller
  @card.run_callbacks :show_page do
    show view, slot_opts
  end
end

#paramsObject



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

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



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

def session
  Env.session
end

#tagged(view, tag) ⇒ Object



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

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

#templateObject



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

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



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

def to_sym
  Card::Format.format_sym self
end