Class: Card::Format

Inherits:
Object show all
Includes:
Nest, Permission, Render, Location
Defined in:
lib/card/format.rb,
lib/card/format/nest.rb,
lib/card/format/render.rb,
lib/card/format/permission.rb

Defined Under Namespace

Modules: Nest, Permission, Render

Constant Summary collapse

DEPRECATED_VIEWS =
{ view: :open, card: :open, line: :closed,
bare: :core, naked: :core }.freeze
INCLUSION_MODES =
{ closed: :closed, closed_content: :closed, edit: :edit,
layout: :layout, new: :edit, setup: :edit,
normal: :normal, template: :template }.freeze
@@registered =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Render

#add_class, #canonicalize_view, #current_view, #default_item_view, #default_render_args, #error_cardname, #hidden_view?, #nest_arg_visibility, #page, #parse_view_visibility, #render, #rendering_error, #rescue_view, #show_view?, #unique_id, #view_method

Methods included from Permission

#approved_view, #ok?, #ok_view, #permitted_view, #view_for_unknown

Methods included from Nest

#fetch_nested_card, #get_nest_content, #get_nest_defaults, #nest, #nest_defaults, #nest_new_args, #prepare_nest, #subformat, #with_nest_mode, #wrap_main

Methods included from Location

#card_path, #card_url, #page_path

Constructor Details

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

~~~~~ INSTANCE METHODS

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/card/format.rb', line 87

def initialize card, opts={}
  raise Card::Error, 'format initialized without card' unless (@card = card)
  opts.each do |key, value|
    instance_variable_set "@#{key}", value
  end

  @mode ||= :normal
  @root ||= self
  @depth ||= 0

  @context_names = get_context_names
  include_set_format_modules
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *opts, &proc) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/card/format.rb', line 172

def method_missing method, *opts, &proc
  if method =~ /(_)?(optional_)?render(_(\w+))?/
    view = Regexp.last_match(3) ? Regexp.last_match(4) : opts.shift
    args = opts[0] ? opts.shift.clone : {}
    if Regexp.last_match(2)
      args[:optional] = true
      args[:default_visibility] = opts.shift
    end
    args[:skip_permissions] = true if Regexp.last_match(1)
    render view, args
  else
    proc = proc { |*a| raw yield *a } if proc
    response = root.template.send method, *opts, &proc
    response.is_a?(String) ? root.template.raw(response) : response
  end
end

Instance Attribute Details

#cardObject (readonly)

Returns the value of attribute card.



24
25
26
# File 'lib/card/format.rb', line 24

def card
  @card
end

#error_statusObject

Returns the value of attribute error_status.



25
26
27
# File 'lib/card/format.rb', line 25

def error_status
  @error_status
end

#formObject

Returns the value of attribute form.



25
26
27
# File 'lib/card/format.rb', line 25

def form
  @form
end

#main_optsObject (readonly)

Returns the value of attribute main_opts.



24
25
26
# File 'lib/card/format.rb', line 24

def main_opts
  @main_opts
end

#nest_optsObject

Returns the value of attribute nest_opts.



25
26
27
# File 'lib/card/format.rb', line 25

def nest_opts
  @nest_opts
end

#parentObject (readonly)

Returns the value of attribute parent.



24
25
26
# File 'lib/card/format.rb', line 24

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



24
25
26
# File 'lib/card/format.rb', line 24

def root
  @root
end

Class Method Details

.extract_class_vars(view, opts) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/card/format.rb', line 41

def extract_class_vars view, opts
  return unless opts.present?
  [:perms, :error_code, :denial, :closed].each do |varname|
    class_var = send varname
    class_var[view] = opts.delete(varname) if opts[varname]
  end
  extract_view_tags view, opts
end

.extract_view_tags(view, opts) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/card/format.rb', line 50

def extract_view_tags view, opts
  tags = opts.delete :tags
  return unless tags
  Array.wrap(tags).each do |tag|
    view_tags[view] ||= {}
    view_tags[view][tag] = true
  end
end

.format_ancestryObject



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

def format_ancestry
  ancestry = [self]
  ancestry += superclass.format_ancestry unless self == Card::Format
  ancestry
end

.format_class_name(format) ⇒ Object



34
35
36
37
38
39
# File 'lib/card/format.rb', line 34

def format_class_name format
  format = format.to_s
  format = '' if format == 'base'
  format = @@aliases[format] if @@aliases[format]
  "#{format.camelize}Format"
end

.max_depthObject



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

def max_depth
  Card.config.max_depth
end

.new(card, opts = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/card/format.rb', line 59

def new card, opts={}
  if self != Format
    super
  else
    format = opts[:format] || :html
    klass = Card.const_get format_class_name(format)
    self == klass ? super : klass.new(card, opts)
  end
end

.register(format) ⇒ Object



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

def register format
  @@registered << format.to_s
end

.tagged(view, tag) ⇒ Object



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

def tagged view, tag
  return unless view && tag && (view_tags = @@view_tags[view.to_sym])
  view_tags[tag.to_sym]
end

Instance Method Details

#add_name_context(name = nil) ⇒ Object



232
233
234
235
236
# File 'lib/card/format.rb', line 232

def add_name_context name=nil
  name ||= card.name
  @context_names += name.to_name.part_names
  @context_names.uniq!
end

#controllerObject



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

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

#focal?Boolean

meaning the current card is the requested card

Returns:

  • (Boolean)


155
156
157
158
159
160
161
# File 'lib/card/format.rb', line 155

def focal? # meaning the current card is the requested card
  if Env.ajax?
    @depth == 0
  else
    main?
  end
end

#format_date(date, include_time = true) ⇒ Object

———— LINKS —————



221
222
223
224
225
226
227
228
229
230
# File 'lib/card/format.rb', line 221

def format_date date, include_time=true
  # using DateTime because Time doesn't support %e on some platforms
  if include_time
    DateTime.new(
      date.year, date.mon, date.day, date.hour, date.min, date.sec
    ).strftime('%B %e, %Y %H:%M:%S')
  else
    DateTime.new(date.year, date.mon, date.day).strftime('%B %e, %Y')
  end
end

#get_content_object(content, opts) ⇒ Object



205
206
207
208
209
210
211
# File 'lib/card/format.rb', line 205

def get_content_object content, opts
  if content.is_a? Content
    content
  else
    Content.new content, self, opts.delete(:content_opts)
  end
end

#get_context_namesObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/card/format.rb', line 102

def get_context_names
  case
  when @context_names
    part_keys = @card.cardname.part_names.map(&:key)
    @context_names.reject { |n| !part_keys.include? n.key }
  when params[:slot]
    context_name_list = params[:slot][:name_context].to_s
    context_name_list.split(',').map(&:to_name)
  else
    []
  end
end

#include_set_format_modulesObject



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

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

#main?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/card/format.rb', line 151

def main?
  @depth == 0
end

#paramsObject



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

def params
  Env.params
end

#process_content(override_content = nil, opts = {}) ⇒ Object

————- Sub Format and Inclusion Processing ————



193
194
195
# File 'lib/card/format.rb', line 193

def process_content override_content=nil, opts={}
  process_content_object(override_content, opts).to_s
end

#process_content_object(override_content = nil, opts = {}) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/card/format.rb', line 197

def process_content_object override_content=nil, opts={}
  content = override_content || render_raw || ''
  content_object = get_content_object content, opts
  content_object.process_each_chunk do |chunk_opts|
    prepare_nest chunk_opts.merge(opts) { yield }
  end
end

#sessionObject



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

def session
  Env.session
end

#showname(title = nil) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/card/format.rb', line 135

def showname title=nil
  if title
    title.to_name.to_absolute_name(card.cardname).to_show(*@context_names)
  else
    @showname ||= card.cardname.to_show(*@context_names)
  end
end

#tagged(view, tag) ⇒ Object



213
214
215
# File 'lib/card/format.rb', line 213

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

#templateObject



163
164
165
166
167
168
169
170
# File 'lib/card/format.rb', line 163

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

#with_name_context(name) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/card/format.rb', line 143

def with_name_context name
  old_context = @context_names
  add_name_context name
  result = yield
  @context_names = old_context
  result
end