Class: EditSnip

Inherits:
Dynasnip show all
Includes:
Login::Helper
Defined in:
lib/vanilla/dynasnips/edit.rb

Overview

The edit dyna will load the snip given in the ‘snip_to_edit’ part of the params

Instance Attribute Summary

Attributes inherited from Vanilla::Renderers::Base

#app

Instance Method Summary collapse

Methods included from Login::Helper

#current_user, #logged_in?, #login_required

Methods inherited from Dynasnip

all, attribute, build_snip, #method_missing, persist!, persist_all!, snip_attributes, snip_name, usage

Methods inherited from Vanilla::Renderers::Base

escape_curly_braces, #include_snips, #initialize, #prepare, #process_text, #raw_content, render, #render, #render_without_including_snips, snip_regexp

Constructor Details

This class inherits a constructor from Vanilla::Renderers::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dynasnip

Instance Method Details

#edit(snip) ⇒ Object



35
36
37
38
39
40
# File 'lib/vanilla/dynasnips/edit.rb', line 35

def edit(snip)
  renderer = Vanilla::Renderers::Erb.new(app)
  renderer.instance_eval { @snip_to_edit = snip } # hacky!
  snip_in_edit_template = renderer.render_without_including_snips(Vanilla.snip('edit'), :template)
  prevent_snip_inclusion(snip_in_edit_template)
end

#get(snip_name = nil) ⇒ Object



11
12
13
14
15
# File 'lib/vanilla/dynasnips/edit.rb', line 11

def get(snip_name=nil)
  return  unless logged_in?
  snip = Vanilla.snip(snip_name || app.request.params[:name])
  edit(snip)
end

#post(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vanilla/dynasnips/edit.rb', line 17

def post(*args)
  return  unless logged_in?
  snip_attributes = cleaned_params
  snip_attributes.delete(:save_button)
  return 'no params' if snip_attributes.empty?
  snip = Vanilla.snip(snip_attributes[:name])
  snip_attributes[:updated_at] = Time.now
  snip_attributes.each do |name, value|
    snip.__send__(:set_value, name, value)
  end
  snip.save
  %{Saved snip #{Vanilla::Routes.link_to snip_attributes[:name]} ok}
rescue Exception => e
  snip_attributes[:created_at] ||= Time.now
  Soup << snip_attributes
  %{Created snip #{Vanilla::Routes.link_to snip_attributes[:name]} ok}
end