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?
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
|