Module: Kojac::ModelMethods

Defined in:
lib/kojac/kojac_rails.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(aClass) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kojac/kojac_rails.rb', line 102

def self.included(aClass)
  aClass.send :extend, ClassMethods
aClass.class_eval do
	scope :by_key, ->(aKey,aOperation=nil) {
		key = if respond_to?(:crack_key)
			crack_key(aKey)
		elsif aClass.respond_to?(:crack_key)
			aClass.crack_key(aKey)
		end
		r = key[:resource]
		id = key[:id]
		a = key[:association]
		if id
			where(id: id)
		else
			where('1 = 1')
		end
	}
end
end

Instance Method Details

#as_json(options = nil) ⇒ Object



185
186
187
# File 'lib/kojac/kojac_rails.rb', line 185

def as_json(options = nil)
	super
end

#kojac_keyObject



168
169
170
# File 'lib/kojac/kojac_rails.rb', line 168

def kojac_key
	self.class.to_s.snake_case.pluralize+'__'+self.id.to_s
end

#unauthorized!(aMessage = nil) ⇒ Object

Raises:

  • (::Pundit::NotAuthorizedError)


164
165
166
# File 'lib/kojac/kojac_rails.rb', line 164

def unauthorized!(aMessage=nil)
 raise ::Pundit::NotAuthorizedError, aMessage||"You are not authorized to perform this action"
end

#update_permitted_attributes!(aChanges, aPolicy) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/kojac/kojac_rails.rb', line 172

def update_permitted_attributes!(aChanges, aPolicy)
	aChanges = KojacUtils.upgrade_hashes_to_params(aChanges)
	p_fields = aPolicy.permitted_fields(:write)
	unauthorized! if p_fields.empty?
	p_fields = aChanges.permit(*p_fields)
	if ::Rails::VERSION::MAJOR <= 3
		assign_attributes(p_fields, :without_protection => true)
	else
		assign_attributes(p_fields)
	end
	save!
end