Module: Ixtlan::Controllers::PhrasesController

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

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

Instance Method Details

#approveObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ixtlan/controllers/phrases_controller.rb', line 124

def approve
  word = TEXT.not_approved(:locale => Locale.default, :code => params[:id]).first
  if word
    word.current_user = current_user
    if word.approve
      word = nil
    else
      render :xml => word.errors, :status => :unprocessable_entity
    end
  end

  # if there was no unapproved word or approval succeeded render
  # the latest approved word
  unless word
    word = Ixtlan::Models::Word.latest_approved(:locale => Locale.default, :code => params[:id]).first
    phrase = word_to_phrase(word, :current_text)
    render :xml => phrase.to_xml, :status => :ok
  end
end

#createObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ixtlan/controllers/phrases_controller.rb', line 56

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ixtlan/controllers/phrases_controller.rb', line 16

def index
  version = params[:version]

  locale = if params[:locale]
             LOCALE.get!(params[:locale])
           else
             LOCALE.default
           end

  phraseMap = {}
  Ixtlan::Models::Word.not_approved(:locale => Locale.default).each do |word|
    phraseMap[word.code] = word_to_phrase(word, :text)
  end
  Ixtlan::Models::Word.latest_approved(:locale => Locale.default).each do |word|
    phrase = phraseMap[word.code]
    if phrase
      merge_word_into_phrase(word, phrase)
    else
      phraseMap[word.code] = word_to_phrase(word, :current_text)
    end
  end
  phrases = ::DataMapper::Collection.new(::DataMapper::Query.new(repository, ::Ixtlan::Models::Phrase), [])
  phraseMap.each { |k,v| phrases << v }
  render :xml => phrases.to_xml
end

#showObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ixtlan/controllers/phrases_controller.rb', line 43

def show
  word = Ixtlan::Models::Word.not_approved(:locale => Locale.default, :code => params[:id]).first
  if word
    phrase = word_to_phrase(word, :text)
    word = Ixtlan::Models::Word.latest_approved(:locale => Locale.default, :code => params[:id]).first
    merge_word_into_phrase(word, phrase) if word
  else
    word = Ixtlan::Models::Word.latest_approved(:locale => Locale.default, :code => params[:id]).first
    phrase = word_to_phrase(word, :current_text)
  end
  render :xml => phrase.to_xml
end

#updateObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ixtlan/controllers/phrases_controller.rb', line 97

def update
  new_text = params[:phrase][:text]
  word = TEXT.not_approved(:locale => Locale.default, :code => params[:id]).first
  if word
    phrase = word_to_phrase(word, :text)
    word_approved = Ixtlan::Models::Word.latest_approved(:locale => Locale.default, :code => params[:id]).first
    merge_word_into_phrase(word_approved, phrase) if word_approved
  else
    # take the latest approved text for the given code and create a new
    # text without version (!= not_approved text) for the given code
    word = TEXT.latest_approved(:locale => Locale.default, :code => params[:id]).first
    phrase = word_to_phrase(word, :current_text)
    word = TEXT.new(:code => params[:id], :locale => Locale.default)
  end

  phrase.text = new_text

  word.text = new_text
  word.current_user = current_user
  if word.save
    render :xml => phrase, :status => :ok
  else
    render :xml => word.errors, :status => :unprocessable_entity
  end

end