Class: Usman::RegistrationDevicesController

Inherits:
ResourceController show all
Defined in:
app/controllers/usman/registration_devices_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/usman/registration_devices_controller.rb', line 29

def create
  @device = @r_object = Device.find_by_id(permitted_params[:id])
  if @device.add_registration(@registration)
    set_notification(true, I18n.t('status.success'), "Registration '#{@registration.name}' has been assigned to the device '#{@device.name}'")
  else
    set_notification(false, I18n.t('status.success'), "Failed to assign the Registration '#{@registration.name}'")
  end
  action_name = params[:action].to_s == "create" ? "new" : "edit"
  render_or_redirect(false, resource_url(@r_object), action_name)
end

#destroyObject



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
65
66
# File 'app/controllers/usman/registration_devices_controller.rb', line 40

def destroy
  @device = @r_object = Device.find_by_id(params[:id])
  if @device
    if @device.remove_registration(@registration)
      get_collections
      set_flash_message(I18n.t('success.deleted'), :success)
      set_notification(true, I18n.t('status.success'), "Registration '#{@registration.name}' has been removed for the device '#{@device.name}'")
      @destroyed = true
    else
      message = I18n.t('errors.failed_to_delete', item: default_item_name.titleize)
      set_flash_message(message, :failure)
      set_notification(false, I18n.t('status.success'), "Failed to remove the Registration '#{@registration.name}'")
      @destroyed = false
    end
  else
    set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize))
  end

  respond_to do |format|
    format.html {}
    format.js  { 
      js_view_path = @resource_options && @resource_options[:js_view_path] ? "#{@resource_options[:js_view_path]}/destroy" : :destroy 
      render js_view_path
    }
  end

end

#indexObject



7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/usman/registration_devices_controller.rb', line 7

def index
  get_collections
  respond_to do |format|
    format.html {}
    format.js  { 
      js_view_path = @resource_options && @resource_options[:js_view_path] ? "#{@resource_options[:js_view_path]}/index" : :index 
      render js_view_path
    }
  end
end

#newObject



24
25
26
27
# File 'app/controllers/usman/registration_devices_controller.rb', line 24

def new
  @device = Device.new
  render_accordingly
end

#showObject



18
19
20
21
22
# File 'app/controllers/usman/registration_devices_controller.rb', line 18

def show
  @device = @r_object = @resource_options[:class].find_by_id(params[:id])
  set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize)) unless @r_object
  render_accordingly
end