Module: Ixtlan::Controllers::TextsController

Defined in:
lib/ixtlan/controllers/texts_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/ixtlan/controllers/texts_controller.rb', line 5

def self.included(base)
  base.cache_headers :protected
end

Instance Method Details

#createObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ixtlan/controllers/texts_controller.rb', line 26

def create
  phrase = params[:phrase]

  # load the locale and delete the locale parameter array
  locale = LOCALE.get!(phrase.delete(:locale)[:code])

  if(TEXT.count(:version => nil, :code => phrase[:code], :locale => locale) == 1)
    logger.warn "precondition failed: " + phrase.inspect
    # mimic created action by just loading it
    render :xml => TEXT.first(:version => nil, :code => phrase[:code], :locale => locale).to_xml, :status => :created
    return
  end

  phrase[:text] ||= phrase.delete(:current_text)

  @text = TEXT.new(phrase)
  if(TEXT.count(:code => phrase[:code], :locale => locale) == 0)
    approve_it = true
  end

  # set the missing attributes
  @text.locale = locale
  @text.current_user = current_user

  respond_to do |format|
    success = @text.save
    if success && approve_it
      @text.current_user = current_user
      success = @text.approve
    end
    if success
      flash[:notice] = 'Text was successfully created.'
      format.html { redirect_to(text_url(@text.id)) }
      format.xml  { render :xml => @text, :status => :created }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @text.errors, :status => :unprocessable_entity }
    end
  end
end

#indexObject



20
21
22
23
24
# File 'lib/ixtlan/controllers/texts_controller.rb', line 20

def index
  version = params[:version]

  @texts = TEXT.all(:locale => LOCALE.get!(params[:locale]))
end

#updateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ixtlan/controllers/texts_controller.rb', line 67

def update
  phrase = params[:phrase]
  phrase[:text] ||= phrase.delete(:current_text)

  respond_to do |format|
    if @text.update(phrase)
      flash[:notice] = phrase[:text].nil? ? 'Text was successfully approved.' : 'Text was successfully updated.'
      format.html { redirect_to(text_url(@text.id)) }
      format.xml  { render :xml => @text, :status => :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @text.errors, :status => :unprocessable_entity }
    end
  end
end