Class: EducodeSales::RolesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
# File 'app/controllers/educode_sales/roles_controller.rb', line 17

def create
  role = Role.new(name: params[:name])
  role.role_areas.build([{ clazz: 'Business' }, { clazz: 'Contract' }, { clazz: 'SalePlan' }, { clazz: 'Teacher' }, { clazz: 'Operation' }, { clazz: 'Customer' }, { clazz: 'MoneyPlan' }, { clazz: 'AssessmentsSetting' }, { clazz: 'Idea' }, { clazz: 'Project' }])
  if role.save
    render_success
  else
    render_failure role
  end
end

#destroyObject



67
68
69
70
71
72
73
# File 'app/controllers/educode_sales/roles_controller.rb', line 67

def destroy
  role = Role.find(params[:id])
  role.destroy
  render_success
rescue ActiveRecord::DeleteRestrictionError => e
  render_failure '该角色已有用户存在,不能再删除'
end

#editObject



31
32
33
34
35
36
# File 'app/controllers/educode_sales/roles_controller.rb', line 31

def edit
  role = Role.find(params[:id])
  @permissions = role.permissions.pluck(:id)
  @areas = role.role_areas.map { |d| "#{d.clazz}-#{d.level}" }
  render layout: false
end

#indexObject

before_action :authenticate_admin



7
8
9
10
11
12
13
14
15
# File 'app/controllers/educode_sales/roles_controller.rb', line 7

def index
  respond_to do |format|
    format.html do
    end
    format.json do
      @roles = Role.page(params[:page]).per(params[:limit])
    end
  end
end

#newObject



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

def new
  render layout: false
end

#renameObject



38
39
40
41
# File 'app/controllers/educode_sales/roles_controller.rb', line 38

def rename
  @name = Role.find(params[:id]).name
  render layout: false
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/educode_sales/roles_controller.rb', line 54

def update
  permissions = []
  role = Role.find(params[:id])
  params[:permission].each do |d|
    d[1].each do |c|
      permissions << Permission.find_by(subject: d[0], action: c[1])
    end
  end
  role.permissions = permissions
  role.save
  render json: { success: true }
end

#update_roleObject



43
44
45
46
47
48
49
50
# File 'app/controllers/educode_sales/roles_controller.rb', line 43

def update_role
  role = Role.find(params[:id])
  if role.update(name: params[:name])
    render_success
  else
    render_failure role
  end
end

#viewObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/educode_sales/roles_controller.rb', line 75

def view
  role = Role.find(params[:id])
  role.role_areas.find_by(clazz: 'Business').update(level: params[:business])
  role.role_areas.find_by(clazz: 'Contract').update(level: params[:contract])
  role.role_areas.find_by(clazz: 'Operation').update(level: params[:operation])
  role.role_areas.find_by(clazz: 'SalePlan').update(level: params[:sale_plan])
  role.role_areas.find_by(clazz: 'Teacher').update(level: params[:teacher])
  r = role.role_areas.find_or_initialize_by(clazz: 'Customer')
  r.level = params[:customer]
  r.save
  r = role.role_areas.find_or_initialize_by(clazz: 'MoneyPlan')
  r.level = params[:money_plan]
  r.save
  r = role.role_areas.find_or_initialize_by(clazz: 'AssessmentsSetting')
  r.level = params[:assessments_setting]
  r.save
  r = role.role_areas.find_or_initialize_by(clazz: 'Idea')
  r.level = params[:idea]
  r.save
  r = role.role_areas.find_or_initialize_by(clazz: 'Project')
  r.level = params[:project]
  r.save
  render_success
end