Class: Grat::Content

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/grat/content.rb,
lib/grat/hwia_patch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#suggested_fieldsObject

Returns the value of attribute suggested_fields.



3
4
5
# File 'lib/grat/content.rb', line 3

def suggested_fields
  @suggested_fields
end

Class Method Details

.find_all_by_tag(tag_name) ⇒ Object



37
38
39
# File 'lib/grat/content.rb', line 37

def self.find_all_by_tag(tag_name)
  all(:conditions => {'tags' => [tag_name]})
end

Instance Method Details

#attributes_for_variablesObject



14
15
16
# File 'lib/grat/content.rb', line 14

def attributes_for_variables
  attributes.reject {|k,v| k == '_id' }
end

#childrenObject



122
123
124
# File 'lib/grat/content.rb', line 122

def children
  @children ||= []
end

#content_with(locals = {}, y = '') ⇒ Object



114
115
116
# File 'lib/grat/content.rb', line 114

def content_with(locals = {},y = '')
  send "#{render_engine}_render", y, content, locals
end

#default_content_varsObject



45
46
47
# File 'lib/grat/content.rb', line 45

def default_content_vars
  super or {}
end

#default_content_vars=(val) ⇒ Object



49
50
51
# File 'lib/grat/content.rb', line 49

def default_content_vars=(val)
  super val.kind_of?(String) ? JSON.parse(val) : val
end

#demo_arrayObject



69
70
71
# File 'lib/grat/content.rb', line 69

def demo_array
  %w(an array is needed)
end

#demo_stringObject



65
66
67
# File 'lib/grat/content.rb', line 65

def demo_string
  'String needed'
end

#detect_default_content_varsObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/grat/content.rb', line 81

def detect_default_content_vars
  counter = 0
  while problem_var = detect_problem_var
    counter += 1
    return false if counter > 200
    (detect_problem_var(problem_var => demo_string) ||
      default_content_vars.merge!(problem_var => demo_string)) or
    (detect_problem_var(problem_var => demo_array) ||
      default_content_vars.merge!(problem_var => demo_array)) or
    raise "Don't know how to reconcile."
  end
end

#detect_problem_var(resolution = {}) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/grat/content.rb', line 105

def detect_problem_var(resolution = {})
  begin
    rendered = content_with(default_content_vars.merge(resolution), problem_var = nil)
    return false
  rescue
    $!.to_s.sub(/.+`/,'').sub(/'.+/,'')
  end
end

#editable_fieldsObject



18
19
20
# File 'lib/grat/content.rb', line 18

def editable_fields
  attributes.reject {|k,v| uneditable_keys.include? k }
end

#erb_render(text, template, vars) ⇒ Object



100
101
102
103
# File 'lib/grat/content.rb', line 100

def erb_render(text, template, vars)
  require 'erb'
  ERB.new(template,0).result(Grat::HashBinding.new(vars).get_binding { text })
end

#haml_render(text, template, vars) ⇒ Object



94
95
96
97
98
# File 'lib/grat/content.rb', line 94

def haml_render(text, template, vars)
  require 'haml'
  haml_template = Haml::Engine.new(template)
  haml_template.render(haml_template, vars) { text }
end

#render_engineObject



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

def render_engine
  if content.match(/\A[!%#.=-]/)
    :haml
  else
    :erb
  end
end

#tagsObject



33
34
35
# File 'lib/grat/content.rb', line 33

def tags
  super or []
end

#tags=(val) ⇒ Object



29
30
31
# File 'lib/grat/content.rb', line 29

def tags=(val)
  super(val.kind_of?(Array) ? val : val.split(' '))
end

#templateObject



53
54
55
# File 'lib/grat/content.rb', line 53

def template
  @template ||= Grat::Content.find_by_url(template_url) if template_url
end

#template=(var) ⇒ Object



57
58
59
# File 'lib/grat/content.rb', line 57

def template=(var)
  raise 'This is probably a mistake.'
end

#template_url=(var) ⇒ Object



61
62
63
# File 'lib/grat/content.rb', line 61

def template_url=(var)
  super(var) unless var.nil? || var.empty?
end

#to_json(state, depth) ⇒ Object



8
9
10
# File 'lib/grat/hwia_patch.rb', line 8

def to_json(state, depth)
  super()
end

#typeObject



41
42
43
# File 'lib/grat/content.rb', line 41

def type
  self.class.to_s.sub(/.+::/, '')
end

#uneditable_keysObject



22
23
24
25
26
27
# File 'lib/grat/content.rb', line 22

def uneditable_keys
  # url is in here so it can maually be placed at the top of edit form.
  # Same deal with template_url
  ["updated_at", "_id", "url", "created_at","content","tags",'template_url','template',
    'default_content_vars','variables_needed','render_engine_name']
end