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



184
185
186
# File 'lib/active_restrictors/active_restrictor.rb', line 184

def _restrictor_custom_user_class
  self.class._restrictor_custom_user_class
end

#allowed_usersObject

Returns User scope with all restrictors applied



189
190
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
# File 'lib/active_restrictors/active_restrictor.rb', line 189

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



169
170
171
# File 'lib/active_restrictors/active_restrictor.rb', line 169

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



163
164
165
# File 'lib/active_restrictors/active_restrictor.rb', line 163

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

#enabled_restrictorsObject

Returns all restrictors taht are currently in enabled status



179
180
181
# File 'lib/active_restrictors/active_restrictor.rb', line 179

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



157
158
159
# File 'lib/active_restrictors/active_restrictor.rb', line 157

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

#implicit_restrictorsObject

Returns restrictors of type :implict



174
175
176
# File 'lib/active_restrictors/active_restrictor.rb', line 174

def implicit_restrictors
  self.class.implicit_restrictors
end

#restrictor_class(hash) ⇒ Object

hash

Restrictor hash

Provides class of restrictor



151
152
153
# File 'lib/active_restrictors/active_restrictor.rb', line 151

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