Class: Editus::HomeController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate, #authenticate!, #set_locale

Instance Method Details

#indexObject



7
8
9
10
# File 'app/controllers/editus/home_controller.rb', line 7

def index
  @scripts = Editus::Script.all.values
  @actions = Editus::Actions.all.reverse
end

#manual_updateObject



39
40
41
# File 'app/controllers/editus/home_controller.rb', line 39

def manual_update
  @model_names = Editus::Client.models
end

#queryObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/editus/home_controller.rb', line 62

def query
  script = Editus::Script.all[params[:id].to_sym]
  method = params[:method].to_sym
  return render json: {status: false, error: :script_not_found}, status: 200 if script.blank?

  unless script.proxy.respond_to?(method)
    return render json: {status: false, error: :undefined_method},
                  status: 200
  end

  result = script.proxy.try(method, *parameters)
  render json: {status: true, data: result}, status: 200
end

#run_scriptObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/editus/home_controller.rb', line 47

def run_script
  script = Editus::Script.all[params[:id].to_sym]
  return redirect_to(root_path) if script.blank?

  action = Editus::Action.new
  action.type = "scripts"
  action.user = 
  action.model_id = script.name
  action.model_name = script.title
  action.changes = script.proxy.up(*parameters) if script.proxy.respond_to?(:up)
  action.save

  redirect_to root_path
end

#scriptsObject



43
44
45
# File 'app/controllers/editus/home_controller.rb', line 43

def scripts
  @scripts = Editus::Script.all.values
end

#undoObject



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
# File 'app/controllers/editus/home_controller.rb', line 76

def undo
  action = Editus::Actions.all.find{|act| act.id == params[:id]}
  return redirect_to(root_path) if action.blank?

  new_action = Editus::Action.new
  new_action.type = "undo"
  new_action.user = 
  new_action.model_id = action.id
  new_action.model_name = action.model_name
  case action.type
  when "models"
    pr = action.changes.transform_values{|(from, _to)| from}
    record = action.model_name.constantize.find(action.model_id)
    record.assign_attributes pr
    new_action.changes = record.changes
    record.update_columns pr
  when "scripts"
    script = Editus::Script.all[action.model_id.to_sym]
    return redirect_to(root_path) if action.blank?

    if script.proxy.respond_to?(:down)
      action.changes.empty? ? script.proxy.down : script.proxy.down(*action.changes)
    end
  end
  new_action.save

  redirect_to root_path
end

#updateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/editus/home_controller.rb', line 23

def update
  @record.assign_attributes user_params
  return redirect_to(root_path) unless @record.changed?

  action = Editus::Action.new
  action.changes = @record.changes
  action.type = "models"
  action.user = 
  action.model_id = @record.id
  action.model_name = @record.klass.class.name
  action.save
  @record.update_columns user_params

  redirect_to root_path
end

#validateObject



12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/editus/home_controller.rb', line 12

def validate
  @record.assign_attributes user_params
  @changes = @record.changes.transform_values do |(from, to)|
    "From #{from.nil? ? 'null' : from} to #{to}"
  end
  @validate_error_flag = !@record.valid?
  @full_messages = @record.errors.full_messages

  render "validate"
end