Module: ScopedAttributes

Extended by:
ActiveSupport::Concern
Defined in:
lib/scoped_attributes.rb,
lib/scoped_attributes/railtie.rb,
lib/scoped_attributes/version.rb,
lib/scoped_attributes/scopable.rb

Defined Under Namespace

Modules: Scopable Classes: Error, Railtie

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#as_jsonObject



50
51
52
# File 'lib/scoped_attributes.rb', line 50

def as_json
  attributes
end

#attributes(include_key: false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/scoped_attributes.rb', line 64

def attributes(include_key: false)
  attributes = {}
  self.class.attributes_registry.each do |name, options|
    only = options[:only]

    if visible?(only)
      attributes[name] = object.public_send(name)
    elsif include_key
      attributes[name] = nil
    end
  end
  attributes
end

#initialize(object, user) ⇒ Object



10
11
12
13
# File 'lib/scoped_attributes.rb', line 10

def initialize(object, user)
  self.object = object
  self.user = user
end

#to_modelObject



54
55
56
57
58
59
60
61
62
# File 'lib/scoped_attributes.rb', line 54

def to_model
  return nil if model.nil?

  if object.try(:id).nil?
    model.new(attributes)
  else
    model.select(attributes.keys).find_by(id: object.id)
  end
end