Class: Permission

Inherits:
Usman::ApplicationRecord show all
Defined in:
app/models/permission.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.save_row_data(hsh) ⇒ Object



30
31
32
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/permission.rb', line 30

def self.save_row_data(hsh)

  return if hsh[:user].blank? || hsh[:feature].blank?

  # Initializing error hash for displaying all errors altogether
  error_object = Kuppayam::Importer::ErrorHash.new

  user = User.find_by_username(hsh[:user].to_s.strip)
  unless user
    summary = "User '#{hsh[:user].to_s.strip}' doesn't exist"
    error_object.errors << { summary: summary }
    return error_object
  end

  feature = Feature.find_by_name(hsh[:feature].to_s.strip)
  
  unless feature
    binding.pry
  end
  
  unless feature
    summary = "Feature '#{hsh[:feature].to_s.strip}' doesn't exist"
    error_object.errors << { summary: summary }
    return error_object
  end

  permission = Permission.where("user_id = ? AND feature_id = ?", user.id, feature.id).first || Permission.new
  permission.user = user
  permission.feature = feature
  permission.can_create = hsh[:can_create].to_s.strip
  permission.can_read = hsh[:can_read].to_s.strip
  permission.can_update = hsh[:can_update].to_s.strip
  permission.can_delete = hsh[:can_delete].to_s.strip
  
  if permission.valid?
    begin
      permission.save!
    rescue Exception => e
      summary = "uncaught #{e} exception while handling connection: #{e.message}"
      details = "Stack trace: #{e.backtrace.map {|l| "  #{l}\n"}.join}"
      error_object.errors << { summary: summary, details: details }        
    end
  else
    summary = "Error while saving permission: #{user.name} - #{feature.name}"
    details = "Error! #{permission.errors.full_messages.to_sentence}"
    error_object.errors << { summary: summary, details: details }
  end
  return error_object
end

Instance Method Details

#can_be_deleted?Boolean

Permission Methods


Returns:

  • (Boolean)


83
84
85
# File 'app/models/permission.rb', line 83

def can_be_deleted?
  true
end

#can_be_edited?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/permission.rb', line 87

def can_be_edited?
  true
end