Module: ActiveRestrictor::InstanceMethods

Defined in:
lib/active_restrictors/active_restrictor.rb

Instance Method Summary collapse

Instance Method Details

#_restrictor_custom_user_classObject

Grabs customizable user class



186
187
188
# File 'lib/active_restrictors/active_restrictor.rb', line 186

def _restrictor_custom_user_class
  self.class._restrictor_custom_user_class
end

#allowed_usersObject

Returns User scope with all restrictors applied



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/active_restrictors/active_restrictor.rb', line 191

def allowed_users
  klass_scope = self.class.restrictor_klass_scoping.where(:id => self.id)
  user_scope = self.class.restrictor_user_scoping
  if(klass_scope.count > 0)
    full_restrictors.each do |restrictor|
      user_scope = user_scope.includes(restrictor[:name])
      unless(restrictor[:user_custom].present?)
        rtable_name = restrictor[:table_name] || restrictor[:class].table_name
        r_scope = self.send(restrictor[:name]).scoped.select("#{rtable_name}.id")
        r_count = self.send(restrictor[:name]).scoped.select("count(*)")
        # this next bit gets rid of the association_name.* rails insists upon and any extra cruft
        r_scope.arel.ast.cores.first.projections.delete_if{|item| item != "#{rtable_name}.id"}
        r_count.arel.ast.cores.first.projections.delete_if{|item| item != "count(*)"}
        user_scope = user_scope.where(
          "#{rtable_name}.id IN (#{r_scope.to_sql})#{
            " OR (#{r_count.to_sql}) = 0" if restrictor[:default_allowed_all]
          }"
        )
      else
        user_scope = restrictor[:user_custom].call(user_scope, self)
      end
    end
    if(Array(implicit_restrictors).size > 0)
      if(r = _restrictor_custom_user_class.reflect_on_all_associations.detect{|r| r.klass == self.class})
        user_scope = user_scope.includes(r.name).merge(klass_scope)
      end
    end
    user_scope
  else
    user_scope.where('null')
  end
end

#basic_model_restrictors(*args) ⇒ Object

Returns restrictors of type :basic_model NOTE: Returns enabled by default. Provide :include_disabled to get all



171
172
173
# File 'lib/active_restrictors/active_restrictor.rb', line 171

def basic_model_restrictors(*args)
  self.class.basic_model_restrictors(*args)
end

#basic_user_restrictors(*args) ⇒ Object

Returns restrictors of type :basic_user NOTE: Returns enabled by default. Provide :include_disabled to get all



165
166
167
# File 'lib/active_restrictors/active_restrictor.rb', line 165

def basic_user_restrictors(*args)
  self.class.basic_user_restrictors(*args)
end

#enabled_restrictorsObject

Returns all restrictors taht are currently in enabled status



181
182
183
# File 'lib/active_restrictors/active_restrictor.rb', line 181

def enabled_restrictors
  self.class.enabled_restrictors
end

#full_restrictors(*args) ⇒ Object

Returns restrictors not of type :basic NOTE: Returns enabled by default. Provide :include_disabled to get all



159
160
161
# File 'lib/active_restrictors/active_restrictor.rb', line 159

def full_restrictors(*args)
  self.class.full_restrictors(*args)
end

#implicit_restrictorsObject

Returns restrictors of type :implict



176
177
178
# File 'lib/active_restrictors/active_restrictor.rb', line 176

def implicit_restrictors
  self.class.implicit_restrictors
end

#restrictor_class(hash) ⇒ Object

hash

Restrictor hash

Provides class of restrictor



153
154
155
# File 'lib/active_restrictors/active_restrictor.rb', line 153

def restrictor_class(hash)
  self.class.restrictor_class(hash)
end