Class: PhrasingPhrasesController

Inherits:
ActionController::Base
  • Object
show all
Includes:
PhrasingHelper
Defined in:
app/controllers/phrasing_phrases_controller.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject



72
73
74
75
76
# File 'app/controllers/phrasing_phrases_controller.rb', line 72

def destroy
  @phrasing_phrase = PhrasingPhrase.find(params[:id])
  @phrasing_phrase.destroy
  redirect_to phrasing_phrases_path, notice: "#{@phrasing_phrase.key} deleted!"
end

#downloadObject



50
51
52
53
54
55
# File 'app/controllers/phrasing_phrases_controller.rb', line 50

def download
  app_name = Rails.application.class.to_s.split("::").first
  app_env = Rails.env
  filename = "phrasing_phrases_#{app_name}_#{app_env}_#{Time.now.strftime("%Y_%m_%d_%H_%M_%S")}.yml"
  send_data PhrasingPhrase.export_yaml, filename: filename
end

#editObject



27
28
29
# File 'app/controllers/phrasing_phrases_controller.rb', line 27

def edit
  @phrasing_phrase = PhrasingPhrase.find(params[:id])
end

#helpObject



78
79
# File 'app/controllers/phrasing_phrases_controller.rb', line 78

def help
end

#import_exportObject



47
48
# File 'app/controllers/phrasing_phrases_controller.rb', line 47

def import_export
end

#indexObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/phrasing_phrases_controller.rb', line 11

def index
  params[:locale] ||= I18n.default_locale
  query = PhrasingPhrase
  query = query.where(locale: params[:locale]) unless params[:locale].blank?

  if params[:search] and !params[:search].blank?
      key_like = PhrasingPhrase.arel_table[:key].matches("%#{params[:search]}%")
      value_like = PhrasingPhrase.arel_table[:value].matches("%#{params[:search]}%")
      @phrasing_phrases = query.where(key_like.or(value_like)).order(:key)
  else
    @phrasing_phrases = query.where("value is not null").order(:key) + query.where("value is null").order(:key)
  end
  
  @locale_names = PhrasingPhrase.uniq.pluck(:locale)
end

#remote_update_phraseObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/phrasing_phrases_controller.rb', line 97

def remote_update_phrase
  klass, attribute = params[:klass], params[:attribute]
  
  if Phrasing.is_whitelisted?(klass, attribute)
    class_object = klass.classify.constantize
    @object = class_object.where(id: params[:id]).first
    @object.send("#{attribute}=",params[:new_value])
    @object.save!
    render :json => @object
  else
    render status: 403, text: "Attribute not whitelisted!"
  end    

  rescue ActiveRecord::RecordInvalid => e
    render status: 403, text: e
end

#syncObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/phrasing_phrases_controller.rb', line 81

def sync
  if Phrasing.staging_server_endpoint.nil?
    redirect_to :back, alert: "You didn't set your source server"
  else
    yaml = read_remote_yaml(Phrasing.staging_server_endpoint)

    if yaml
      PhrasingPhrase.import_yaml(yaml)
      redirect_to :back, notice: "Translations synced from source server"
    else
      redirect_to :back
    end

  end
end

#updateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/phrasing_phrases_controller.rb', line 31

def update
  @phrasing_phrase = PhrasingPhrase.find(params[:id])
  @phrasing_phrase.value = params[:phrasing_phrase][:value]
  @phrasing_phrase.save!

  respond_to do |format|
    format.html do
      redirect_to phrasing_phrases_path, notice: "#{@phrasing_phrase.key} updated!"    
    end

    format.js do
      render :json => @phrasing_phrase
    end
  end
end

#uploadObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/phrasing_phrases_controller.rb', line 57

def upload
    number_of_changes = PhrasingPhrase.import_yaml(params["file"].tempfile)
    redirect_to phrasing_phrases_path, notice: "YAML file uploaded successfully! Number of phrases changed: #{number_of_changes}."
  rescue Exception => e
    logger.info "\n#{e.class}\n#{e.message}"
    message = if params[:file].nil?
      "Please choose a file."
    else
      "Please upload a valid YAML file."
    end

    flash[:alert] = "There was an error processing your upload! #{message}"
    render action: 'import_export', status: 400
end