Guest User

Untitled

a guest
Jan 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. ### Dynamic Permissions
  2.  
  3. ```User < ActiRecord::Base
  4. has_many :roles
  5. has_many :permission, through: :roles
  6. has_many :user_roles
  7. has_many :custom_permissions
  8. end
  9.  
  10. UserRole < Base
  11. belongs_to :user
  12. belongs_to :role
  13. end
  14.  
  15. Role < Base
  16. has_many :permissions
  17. has_many :users
  18. end
  19.  
  20. RolePermission < Base
  21. belongs_to :role
  22. belongs_to :permission
  23. end
  24.  
  25. CustomPermission < Base
  26. belongs_to :user
  27. belongs_to :permission
  28. end
  29.  
  30. Permission < Base
  31. has_many :roles
  32. end
  33.  
  34. user.roles = [role1, role2]
  35. user.save
  36.  
  37. user.roles << new_role
  38. ```
  39.  
  40. ### Cache class
  41. ```
  42. class Cache
  43. @@storage = {}
  44.  
  45. def self.set(key,value)
  46. @@storage[key] = StoredObject.new(value, 1.day.from_now)
  47. end
  48. def self.get(key)
  49. element = @@storage[key]
  50. if !element.nil?
  51. if element.expired?
  52. @@storage.delete(key)
  53. return nil
  54. else
  55. return element.value
  56. end
  57. end
  58. end
  59. def self.size
  60. @@storage.size
  61. end
  62.  
  63. def self.delete(key)
  64. @@storage.delete(key)
  65. end
  66. end
  67.  
  68. class StoredObject
  69. initialize(value, expiration_date)
  70. @value = value
  71. @expiration_date = expiration_date
  72. end
  73.  
  74. private
  75.  
  76. def expired?
  77. expiration_date < Date.today
  78. end
  79.  
  80. attr_reader :value, expiration_date
  81. end
  82.  
  83. ```
  84.  
  85. ### MySQL
  86. ```SELECT AVG(e.salary), p.Department as average_salary from Project p left join Members m on m.project_id = p.project_id left join Employee e on e.id = m.employee_id group by p.Department, e.id;```
Add Comment
Please, Sign In to add comment