Class: Grat::Application

Inherits:
Sinatra::Base
  • Object
show all
Includes:
System
Defined in:
lib/grat.rb

Instance Method Summary collapse

Methods included from System

#content, #focus_params, #missing_page, #model, #new_content, #request_type, #template_chain, #url

Instance Method Details

#combine_docs(text, template, vars) ⇒ Object



172
173
174
# File 'lib/grat.rb', line 172

def combine_docs(text,template,vars)
  template.content_with(vars,text)
end

#delete_path(url) ⇒ Object



168
169
170
# File 'lib/grat.rb', line 168

def delete_path(url)
  "/__admin/delete#{url}"
end

#edit_path(url) ⇒ Object



164
165
166
# File 'lib/grat.rb', line 164

def edit_path(url)
  "/__admin/edit#{url}"
end

#file_import_textObject



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

def file_import_text
  if params[:import] && params[:import][:file] && params[:import][:file][:tempfile]
    params[:import][:file][:tempfile].read
  end
end

#haml(*args) ⇒ Object



176
177
178
179
# File 'lib/grat.rb', line 176

def haml(*args)
  require 'haml'
  super(*args)
end

#import(json_text, strategy) ⇒ Object

Rather inefficient at present.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/grat.rb', line 46

def import(json_text, strategy)
  json_to_import = JSON.parse json_text
  if json_to_import.is_a? Array
    model.delete_all if strategy == 'demolish'
    json_to_import.each do |record|
      case strategy
      when 'add'
        import_record record
      when 'replace'
        import_record record, {:replace => true, :check=> true}
      when 'demolish'
        import_record record, {:replace => true, :check => false}
      end
    end
  end
end

#import_record(hash, options = {:replace => false, :check => true}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/grat.rb', line 63

def import_record(hash, options = {:replace => false, :check => true})
  record = model.find_by_url(hash['url']) if options[:check]
  if record && options[:replace]
    record.destroy if record
  end
  unless record && !options[:replace]
    model.create hash
  end
end

#next_content_urlObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/grat.rb', line 125

def next_content_url
  current_content = content
  new_url = '/broken'
  while current_content
    url         = current_content.url
    number      = url[/\d*\Z/]
    next_number = number.to_i + 1
    new_url = url.sub(/\d*\Z/, next_number.to_s)
    current_content = model.find_by_url(url.sub(/\d*\Z/, next_number.to_s))
  end
  if content.template_url
    new_url + '?template=' + content.template_url
  else
    new_url
  end
end

#templatesObject



181
182
183
# File 'lib/grat.rb', line 181

def templates
  model.find_all_by_tag('template')
end