Module: YiffSpace::Concerns::UserMethods::ClassMethods
- Defined in:
- lib/yiffspace/concerns/user_methods.rb
Instance Method Summary collapse
- #belongs_to_user ⇒ Object
- #create_with ⇒ Object
- #create_with! ⇒ Object
- #new_with ⇒ Object
- #resolvable ⇒ Object
- #with_user(instance, method, user, *args) ⇒ Object
Instance Method Details
#belongs_to_user ⇒ Object
9 10 11 |
# File 'lib/yiffspace/concerns/user_methods.rb', line 9 def belongs_to_user(*, **) UserAttribute.new(self, *, **, db: true) end |
#create_with ⇒ Object
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_with ⇒ Object
37 38 39 |
# File 'lib/yiffspace/concerns/user_methods.rb', line 37 def new_with(...) with_user(self, :new, ...) end |
#resolvable ⇒ Object
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! = [attrs.index_with(user), *hashes, *params.map(&:to_hash)].inject(:merge) if %i[destroy destroy!].include?(method) .each do |attr, value| instance.send("#{attr}=", value) end instance.send(method) else instance.send(method, *other, **, **, &) end end |