Class: Card::View

Inherits:
Object show all
Extended by:
Cache::ClassMethods
Includes:
Cache, Classy, Options, Permission
Defined in:
card/lib/card/view.rb,
card/lib/card/view/cache.rb,
card/lib/card/view/classy.rb,
card/lib/card/view/options.rb,
card/lib/card/view/cache/stub.rb,
card/lib/card/view/permission.rb,
card/lib/card/view/options/voo_api.rb,
card/lib/card/view/options/key_lists.rb,
card/lib/card/view/cache/cache_action.rb,
card/lib/card/view/options/visibility.rb

Overview

Card::View manages view options, view caching, and view permissions.

View objects, which are instantiated whenever a view is rendered, are available as in views and other format methods. The view objects can be accessed using #voo. We sometimes feebly pretend VOO is an acronym for “view option object,” but really we just needed a way not to confuse these Card::View options with the countless references to viewnames that naturally arise when rendering views within views within views.

When view A renders view B within the same format object, A’s voo is the parent of B’s voo. When card C nests card D, a new (sub)format object is initialized. C is then the parent format of D, but D has its own root voo.

So a lineage might look something like this:

F1V1 -> F1V2 -> F1V3 -> F2V1 -> F2V2 -> F3V1 ...

Defined Under Namespace

Modules: Cache, Classy, Options, Permission

Constant Summary

Constants included from Permission

Permission::CRUD

Constants included from Cache

Cache::EXPIRE_VALUES

Constants included from Cache::CacheAction

Cache::CacheAction::ACTIVE_CACHE_LEVEL

Constants included from Options::Visibility

Options::Visibility::VIZ_SETTING

Instance Attribute Summary collapse

Attributes included from Options::VooApi

#normalized_options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cache::ClassMethods

cache, caching, caching?

Methods included from Permission

#view_perms

Methods included from Classy

#add_extra_classes, #class_down, #class_up, #classy, #deep_extra_classes, #extra_classes, #remove_extra_classes, #with_class_up, #without_upped_class

Methods included from Options

add_option

Methods included from Options::KeyLists

#accessible_keys, #all_keys, #heir_keys, #reset_key_lists, #shark_keys, #slot_keys

Methods included from Options::Visibility

#explicit_show?, #hide, #hide!, #hide?, #optional?, #process_visibility, #show, #show!, #show?, #visible?, #viz, #viz_hash

Methods included from Options::VooApi

define_getter, define_setter, included, #items, #normalize_cache, #normalize_edit, #normalize_input_type, #normalize_special_options!, #normalize_wrap, #slot_options, #special_option_value

Constructor Details

#initialize(format, view, raw_options = {}, parent = nil) ⇒ View

Returns a new instance of View.

Parameters:

  • format (Card::Format)
  • view (Symbol)

    viewname. Note: Card::View is initialized without a view when voo is called outside of a render, eg subformat(cardname).method_with_voo_reference.

  • raw_options (Hash) (defaults to: {})
  • parent (Card::View) (defaults to: nil)

    (optional)



56
57
58
59
60
61
62
63
64
# File 'card/lib/card/view.rb', line 56

def initialize format, view, raw_options={}, parent=nil
  @format = format
  @raw_view = view
  @raw_options = raw_options
  @parent = parent

  @card = @format.card
  normalize_options
end

Instance Attribute Details

#cardObject (readonly)

Returns the value of attribute card.



29
30
31
# File 'card/lib/card/view.rb', line 29

def card
  @card
end

#formatObject (readonly)

Returns the value of attribute format.



29
30
31
# File 'card/lib/card/view.rb', line 29

def format
  @format
end

#interiorObject

Returns the value of attribute interior.



30
31
32
# File 'card/lib/card/view.rb', line 30

def interior
  @interior
end

#parentObject (readonly)

Returns the value of attribute parent.



29
30
31
# File 'card/lib/card/view.rb', line 29

def parent
  @parent
end

Class Method Details

.normalize(view) ⇒ Symbol

Returns viewname as Symbol.

Returns:

  • (Symbol)

    viewname as Symbol



34
35
36
# File 'card/lib/card/view.rb', line 34

def normalize view
  view.present? ? view.to_sym : nil
end

.normalize_list(val) ⇒ Array

Returns list of viewnames as Symbols.

Returns:

  • (Array)

    list of viewnames as Symbols



39
40
41
42
43
44
45
46
47
# File 'card/lib/card/view.rb', line 39

def normalize_list val
  case val
  when NilClass then []
  when Array    then val
  when String   then val.split(/[\s,]+/)
  when Symbol   then [val]
  else raise Card::Error, "bad show/hide argument: #{val}"
  end
end

Instance Method Details

#deep_rootObject

the root voo of the root format



100
101
102
# File 'card/lib/card/view.rb', line 100

def deep_root
  format.root.voo
end

#deep_root?true/false

neither view nor format has a parent

Returns:

  • (true/false)


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

def deep_root?
  !parent && !format.parent
end

#depthObject



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

def depth
  @depth ||= parent ? (parent.depth + 1) : 0
end

#next_ancestor(across_format = true) ⇒ Card::View

next voo object found tracing ancestry through parent voos and/or parent formats

Returns:



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

def next_ancestor across_format=true
  parent || (across_format && next_format_ancestor) || nil
end

#next_format_ancestorObject

voo object of format’s parent



121
122
123
# File 'card/lib/card/view.rb', line 121

def next_format_ancestor
  format.parent&.voo
end

#ok_viewSymbol

the final view. can be different from @requested_view when there are issues with permissions, recursions, unknown cards, etc.

Returns:

  • (Symbol)

    view name



85
86
87
# File 'card/lib/card/view.rb', line 85

def ok_view
  @ok_view ||= format.monitor_depth { altered_view || requested_view }
end

#processrendered view or a stub

handle rendering, including optional visibility, permissions, and caching

Returns:

  • (rendered view or a stub)


68
69
70
71
72
73
# File 'card/lib/card/view.rb', line 68

def process
  return if process_live_options == :hide

  # Rails.logger.debug "processing #{card.name} / #{ok_view}"
  fetch { yield ok_view }
end

#requested_viewSymbol

the view to “attempt”. Typically the same as @raw_view, but @raw_view can be overridden, eg for the main view (top view of the main card on a page)

Returns:

  • (Symbol)

    view name



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

def requested_view
  @requested_view ||= View.normalize live_options[:view]
end

#rootCard::View

Returns:



90
91
92
# File 'card/lib/card/view.rb', line 90

def root
  @root = parent ? parent.root : self
end

#root?true/false

Returns:

  • (true/false)


95
96
97
# File 'card/lib/card/view.rb', line 95

def root?
  !parent
end