Module: TentD::Model::Permissible::ClassMethods
- Defined in:
- lib/tentd/model/permissible.rb
Instance Method Summary collapse
- #fetch_all(params) ⇒ Object
- #fetch_with_permissions(params, current_auth, &block) ⇒ Object
- #find_with_permissions(id, current_auth) ⇒ Object
- #query_with_permissions(current_auth, params = Hashie::Mash.new) ⇒ Object
Instance Method Details
#fetch_all(params) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/tentd/model/permissible.rb', line 112 def fetch_all(params) params = Hashie::Mash.new(params) unless params.kind_of?(Hashie::Mash) query = [] query_conditions = [] query_bindings = [] if params.return_count query << "SELECT COUNT(#{table_name}.*) FROM #{table_name}" else query << "SELECT #{table_name}.* FROM #{table_name}" end if params.since_id query_conditions << "#{table_name}.id > ?" query_bindings << params.since_id.to_i end if params.before_id query_conditions << "#{table_name}.id < ?" query_bindings << params.before_id.to_i end if params.entity query_conditions << "#{table_name}.entity IN ?" query_bindings << Array(params.entity) end if block_given? yield params, query_conditions, query_bindings end query_conditions << "#{table_name}.user_id = ?" query_bindings << User.current.id query_conditions << "#{table_name}.deleted_at IS NULL" query << "WHERE #{query_conditions.join(' AND ')}" unless params.return_count query << "ORDER BY id DESC" unless query.find { |q| q =~ /^order/i } query << "LIMIT ?" query_bindings << [(params.limit ? params.limit.to_i : TentD::API::PER_PAGE), TentD::API::MAX_PER_PAGE].min end if params.return_count DataMapper.repository(:default).adapter.send(:with_connection) do |connection| connection.create_command(query.join(' ')).execute_reader(*query_bindings).to_a.first['count'] end else find_by_sql([query.join(' '), *query_bindings]) end end |
#fetch_with_permissions(params, current_auth, &block) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/tentd/model/permissible.rb', line 167 def (params, current_auth, &block) params = Hashie::Mash.new(params) unless params.kind_of?(Hashie::Mash) (current_auth, params) do |query, query_bindings| if params.since_id query << "AND #{table_name}.id > ?" query_bindings << params.since_id.to_i end if params.before_id query << "AND #{table_name}.id < ?" query_bindings << params.before_id.to_i end if params.entity query << "AND #{table_name}.entity IN ?" query_bindings << Array(params.entity) end if block_given? yield params, query, query_bindings end unless params.return_count query << "ORDER BY id DESC" unless query.find { |q| q =~ /^order/i } query << "LIMIT ?" query_bindings << [(params.limit ? params.limit.to_i : TentD::API::PER_PAGE), TentD::API::MAX_PER_PAGE].min end if params.return_count DataMapper.repository(:default).adapter.send(:with_connection) do |connection| connection.create_command(query.join(' ')).execute_reader(*query_bindings).to_a.first['count'] end else find_by_sql([query.join(' '), *query_bindings]) end end end |
#find_with_permissions(id, current_auth) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/tentd/model/permissible.rb', line 100 def (id, current_auth) (current_auth) do |query, query_bindings| query << "AND #{table_name}.id = ?" query_bindings << id query << "LIMIT 1" records = find_by_sql([query.join(' '), *query_bindings]) records.first end end |
#query_with_permissions(current_auth, params = Hashie::Mash.new) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tentd/model/permissible.rb', line 58 def (current_auth, params=Hashie::Mash.new) query = [] query_bindings = [] if params.return_count query << "SELECT COUNT(#{table_name}.*) FROM #{table_name}" else query << "SELECT #{table_name}.* FROM #{table_name}" end if current_auth && current_auth.respond_to?(:permissible_foreign_key) query << "LEFT OUTER JOIN permissions ON permissions.#{} = #{table_name}.id" query << "AND (permissions.#{current_auth.permissible_foreign_key} = ?" query_bindings << current_auth.id if current_auth.respond_to?(:groups) && current_auth.groups.to_a.any? query << "OR permissions.group_public_id IN ?)" query_bindings << current_auth.groups else query << ")" end query << "WHERE (public = ? OR permissions.#{} = #{table_name}.id)" query_bindings << true else query << "WHERE public = ?" query_bindings << true end query << "AND user_id = ?" query_bindings << User.current.id query << "AND #{table_name}.deleted_at IS NULL" if properties[:original] query << "AND original = ?" query_bindings << true end if block_given? yield query, query_bindings end end |