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



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

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

#downloadObject



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

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 Phrasing::Serializer.export_yaml, filename: filename
end

#editObject



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

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

#helpObject



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

def help
end

#import_exportObject



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

def import_export
end

#indexObject



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

def index
  params[:locale] ||= I18n.default_locale
  query = PhrasingPhrase
  query = query.order("#{query.table_name}.key")
  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))
  else
    @phrasing_phrases = query.where("value is not null") + query.where("value is null")
  end
  
  @locale_names = PhrasingPhrase.uniq.pluck(:locale)
end

#remote_update_phraseObject



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

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



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

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
      Phrasing::Serializer.import_yaml(yaml)
      redirect_to :back, notice: "Translations synced from source server"
    else
      redirect_to :back
    end

  end
end

#updateObject



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

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



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

def upload
    number_of_changes = Phrasing::Serializer.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