Module: YiffSpace::Concerns::ApiMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/yiffspace/concerns/api_methods.rb

Defined Under Namespace

Modules: ClassMethods Classes: NotVisibleError

Instance Method Summary collapse

Instance Method Details

#api_attributes(user) ⇒ Object

XXX deprecated, shouldn’t expose this as an instance method.



30
31
32
33
# File 'lib/yiffspace/concerns/api_methods.rb', line 30

def api_attributes(user)
  policy = Pundit.policy(user, self) || ApplicationPolicy.new(user, self)
  policy.api_attributes
end

#deep_reject_hash(hash, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/yiffspace/concerns/api_methods.rb', line 88

def deep_reject_hash(hash, &block)
  hash.each_with_object({}) do |(key, value), result|
    if value.is_a?(Hash)
      result[key] = deep_reject_hash(value, &block)
    elsif value.is_a?(Array)
      result[key] = value.map { |v| v.is_a?(Hash) ? deep_reject_hash(v, &block) : v }.reject { |i| block.call(nil, i) }
    elsif !block.call(key, value)
      result[key] = value
    end
  end
end

#html_data_attributes(user) ⇒ Object

XXX deprecated, shouldn’t expose this as an instance method.



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

def html_data_attributes(user)
  policy = Pundit.policy(user, self) || ApplicationPolicy.new(user, self)
  policy.html_data_attributes
end

#process_api_attributes(options, underscore: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yiffspace/concerns/api_methods.rb', line 41

def process_api_attributes(options, underscore: false)
  options[:methods] ||= []
  attributes, methods = api_attributes(options[:user]).partition { |attr| has_attribute?(attr) }
  methods += options[:methods]
  if underscore && options[:only].blank?
    options[:only] = attributes + methods
  else
    options[:only] ||= attributes + methods
  end

  attributes &= options[:only]
  methods &= options[:only]

  options[:only] = attributes
  options[:methods] = methods

  options.delete(:methods) if options[:methods].empty?
  options
end

#serializable_hash(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/yiffspace/concerns/api_methods.rb', line 61

def serializable_hash(options = {})
  options ||= {}
  options[:user] ||= CurrentUser.user || User.anonymous
  return :not_visible unless visible?(options[:user])

  if options[:only].is_a?(String)
    options.delete(:methods)
    options.delete(:include)
    options.merge!(ParameterBuilder.serial_parameters(options[:only], self, options))
    if options[:only].include?("_")
      options[:only].delete("_")
      options = process_api_attributes(options, underscore: true)
    end
  else
    options = process_api_attributes(options)
  end
  options[:only] += [SecureRandom.hex(6)]

  hash = super
  hash.transform_keys! { |key| key.delete("?").delete_prefix("apionly_") }
  deep_reject_hash(hash) { |_, v| v == :not_visible }
end

#visible?(_user) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/yiffspace/concerns/api_methods.rb', line 84

def visible?(_user)
  true
end