Class: TranslationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/translations_controller.rb

Overview

The translations controller.

Instance Method Summary (collapse)

Instance Method Details

- (Object) create

POST /translations POST /translations.json



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
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/translations_controller.rb', line 57

def create
  @translation_langs = TranslationsHelper.supported_language_codes_with_names
  @tag_id = params['translation']['tag_id']
  @translation = Translation.new()
  @editTagForm = false

  unless @tag_id.nil?
    tag = Tag.find_by_id(@tag_id)
    text_record = tag.text_record
    result = false

    if text_record.nil?
      # if there is no text record create a new one

      @editTagForm = true
      original_text = Translation.create(translation_params)
      result = original_text.save

      if result
        text_record = TextRecord.new(original_text: original_text, gender: 'm')
        result = text_record.save

        if result
          tag.text_record = text_record
        end
      end
    else
      original_text = text_record.original_text.text
      original_lang_code = text_record.original_text.lang_code

      new_lang_code = params[:translation][:lang_code].to_s

      unless new_lang_code.nil? or new_lang_code.empty?
        new_text = translate_text(original_text, original_lang_code, new_lang_code)

        @translation = Translation.new(text: new_text, lang_code: new_lang_code)
        result = @translation.save
        text_record.translations << @translation
      end
    end
  end

  respond_to do |format|
    if result
      format.html { redirect_to tag_path(@tag_id), notice: 'Translation was successfully created.' }
      format.json { render action: 'show', status: :created, location: @translation }
    else
      format.html { render action: 'new' }
      format.json { render json: @translation.errors, status: :unprocessable_entity }
    end
  end
end

- (Object) destroy

DELETE /translations/1 DELETE /translations/1.json



130
131
132
133
134
135
136
137
138
# File 'app/controllers/translations_controller.rb', line 130

def destroy
  tag_id = params['tag_id']

  @translation.destroy
  respond_to do |format|
    format.html { redirect_to tag_path(tag_id), notice: 'Translation was successfully removed.' }
    format.json { head :no_content }
  end
end

- (Object) edit

GET /translations/1/edit



49
50
51
52
53
# File 'app/controllers/translations_controller.rb', line 49

def edit
  @tag_id = params['tag_id']
  @translation_langs = TranslationsHelper.supported_language_codes_with_names
  @editTagForm = true
end

- (Object) index

GET /translations GET /translations.json



9
10
11
12
13
14
15
16
# File 'app/controllers/translations_controller.rb', line 9

def index
  if current_customer.role == 'admin'
    @translations = Translation.all
  else
    # output customers translations
    @translations = Translation.joins(text_record: [:tag]).where('Tags.customer_id' => current_customer)
  end
end

- (Object) new

GET /translations/new



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/translations_controller.rb', line 24

def new
  @translation = Translation.new
  @tag_id = params['tag_id']
  @translation_langs = TranslationsHelper.supported_language_codes_with_names

  # if this is the first translation show a text input field
  unless @tag_id.nil?
    tag = Tag.find_by_id(@tag_id)
    text_record = tag.text_record

    if text_record.nil?
      @editTagForm = true
    else
      translations = text_record.translations

      if translations.nil?
        @editTagForm = true
      else
        @editTagForm = false
      end
    end
  end
end

- (Object) show

GET /translations/1 GET /translations/1.json



20
21
# File 'app/controllers/translations_controller.rb', line 20

def show
end

- (Object) update

PATCH/PUT /translations/1 PATCH/PUT /translations/1.json



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/translations_controller.rb', line 112

def update
  @translation_langs = TranslationsHelper.supported_language_codes_with_names
  @tag_id = params['translation']['tag_id']
  @editTagForm = true

  respond_to do |format|
    if @translation.update(translation_params)
      format.html { redirect_to tag_path(@tag_id), notice: 'Translation was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @translation.errors, status: :unprocessable_entity }
    end
  end
end