Module: YiffSpace::Concerns::UserMethods::ClassMethods

Defined in:
lib/yiffspace/concerns/user_methods.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_userObject



9
10
11
# File 'lib/yiffspace/concerns/user_methods.rb', line 9

def belongs_to_user(*, **)
  UserAttribute.new(self, *, **, db: true)
end

#create_withObject



41
42
43
# File 'lib/yiffspace/concerns/user_methods.rb', line 41

def create_with(...)
  new_with(...).tap(&:save)
end

#create_with!Object



45
46
47
# File 'lib/yiffspace/concerns/user_methods.rb', line 45

def create_with!(...)
  new_with(...).tap(&:save!)
end

#new_withObject



37
38
39
# File 'lib/yiffspace/concerns/user_methods.rb', line 37

def new_with(...)
  with_user(self, :new, ...)
end

#resolvableObject



13
14
15
# File 'lib/yiffspace/concerns/user_methods.rb', line 13

def resolvable(*, **)
  UserAttribute.new(self, *, **, db: false)
end

#with_user(instance, method, user, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/yiffspace/concerns/user_methods.rb', line 17

def with_user(instance, method, user, *args, **, &)
  default = { new: :creator, create: :creator, create!: :creator, update: :updater, update!: :updater, destroy: :destroyer, destroy!: :destroyer }.fetch(method)
  attrs = args.grep(Symbol)
  attrs = [default] if default && attrs.empty?
  other = args - attrs
  hashes = other.grep(Hash)
  params = other.grep(ActionController::Parameters)
  other -= hashes + params
  other.compact_blank!
  options = [attrs.index_with(user), *hashes, *params.map(&:to_hash)].inject(:merge)
  if %i[destroy destroy!].include?(method)
    options.each do |attr, value|
      instance.send("#{attr}=", value)
    end
    instance.send(method)
  else
    instance.send(method, *other, **options, **, &)
  end
end