Created
August 21, 2013 20:03
-
-
Save bleakwood/6299470 to your computer and use it in GitHub Desktop.
Each post is created with a role, which represents a data type. A user can only access certain data types another user shared with him. Each type of data is assigned a binary code for permission, which is assigned with a group where user can decides permissions. One user can belong to multiple groups of another user. self.for_user is trying to f…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
attr_accessible :user, :description, :role, :guid, :created_at, :group, :gpoint | |
belongs_to :user | |
has_many :notifications, :as => :notifiable | |
has_data_fields [:gpoint, :group] | |
validates_presence_of :user_id, :description | |
validates_uniqueness_of :guid, :scope => :user_id, :if => Proc.new{|x| !x.guid.blank? } | |
attr_accessible :post_type, :description, :user_id, :location | |
def as_json(options={}) | |
{ | |
:description => description, | |
:created_at => created_at, | |
:post_type => post_type, | |
:group => group, | |
:gpoint => gpoint, | |
:user => user | |
} | |
end | |
def todays_post_of_same_type_exists | |
Post.where(:user_id => user.id, :post_type => post_type).each do |post| | |
if post.created_at.to_date == Date.today | |
self.errors.add(:base, "Post of today already exists") | |
end | |
end | |
end | |
def self.for_user(user) | |
role_matchers = user.cumulative_roles.map do |user_id, cr| | |
[user_id, cr.split('').map{|role| role == "1" ? "#{[0,1]}" : "0"}.join('')] | |
end | |
query_operator = Rails.env.production? ? "~" : "REGEXP" | |
query = role_matchers.map do |user_id, matcher| | |
"(user_id = #{user_id} and role #{query_operator} '#{matcher}')" | |
end | |
query << "(user_id = #{user.id})" | |
Post.where(query.join(" OR ")).order("updated_at DESC") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment