Class: Admin::Muck::AccessCodesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/admin/muck/access_codes_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulkObject



27
28
29
30
31
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 27

def bulk
  @access_code = AccessCode.new(:expires_at => 1.year.since)
  @access_code_requests_count = AccessCodeRequest.unfullfilled.count
  render :template => 'admin/access_codes/bulk'
end

#bulk_createObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 33

def bulk_create
  @access_code = AccessCode.new(params[:access_code])
  @access_code.bulk_valid?
  if send_requests = @access_code.send_requests
    access_code_requests = AccessCodeRequest.get_requests(@access_code.send_request_limit)
    emails = access_code_requests.map(&:email)
  else
    emails = @access_code.emails.split(',')
  end
  use_random_code = @access_code.code.blank?
  @access_code.save! unless use_random_code
  emails.each do |email|
    if use_random_code
      # need to build a new access code for each email
      @access_code = AccessCode.new(params[:access_code])
      @access_code.unlimited = false
      @access_code.use_limit = 1
      @access_code.uses = 0
      @access_code.code = AccessCode.random_code
      @access_code.sent_to = email
      @access_code.save!
    end
    UserMailer.access_code(email, @access_code.subject, @access_code.message, @access_code.code).deliver
  end
  AccessCodeRequest.mark_fullfilled(access_code_requests) if access_code_requests
  flash[:notice] = translate('muck.users.bulk_access_codes_created', :email_count => emails.count)
  redirect_to bulk_admin_access_codes_path

rescue ActiveRecord::RecordInvalid => ex
  @access_code_requests_count = AccessCodeRequest.unfullfilled.count
  render :template => "admin/access_codes/bulk"
end

#createObject



18
19
20
21
22
23
24
25
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 18

def create
  @access_code = AccessCode.new(params[:access_code])
  if @access_code.save
    ajax_create_access_code
  else
    output_admin_messages(@access_code)
  end
end

#destroyObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 80

def destroy
  @access_code = AccessCode.find(params[:id])
  if @access_code.users.length <= 0
    success = @access_code.destroy
    flash[:notice] = translate('muck.users.access_code_delete_error') unless success
  else
    flash[:notice] = translate('muck.users.access_code_delete_problem')
  end
  respond_to do |format|
    format.html { redirect_to admin_access_codes_path }
    format.js do
      if success
        render :template => 'admin/access_codes/destroy', :layout => false
      else
        output_admin_messages
      end
    end
  end
  
end

#editObject



66
67
68
69
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 66

def edit
  @access_code = AccessCode.find(params[:id])
  render :template => "admin/access_codes/edit", :layout => false
end

#indexObject



3
4
5
6
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 3

def index
  @codes = AccessCode.by_newest.includes(:provided_by, :users).paginate(:page => @page, :per_page => @per_page)
  render :template => 'admin/access_codes/index'
end

#newObject



13
14
15
16
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 13

def new
  @access_code = AccessCode.new
  render :template => 'admin/access_codes/new', :layout => false
end

#showObject



8
9
10
11
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 8

def show
  @access_code = AccessCode.find(params[:id])
  render :template => 'admin/access_codes/show'
end

#updateObject



71
72
73
74
75
76
77
78
# File 'app/controllers/admin/muck/access_codes_controller.rb', line 71

def update
  @access_code = AccessCode.find(params[:id])
  if @access_code.update_attributes(params[:access_code])
    ajax_update_access_code
  else
    output_admin_messages(@access_code)
  end
end