Module: Entangled::Model::InstanceMethods

Includes:
Helpers
Defined in:
lib/entangled/model.rb

Instance Method Summary collapse

Methods included from Helpers

#redis

Instance Method Details

#as_json(options = nil) ⇒ Object

Override the as_json method so that the JSON representation of the resource includes its errors. This is necessary so that errors are sent back to the client along with the resource on create and update



72
73
74
# File 'lib/entangled/model.rb', line 72

def as_json(options = nil)
  super(options || attributes).merge(errors: errors).as_json
end

#channels(tail = '') ⇒ Object

Build channels. Channels always at least include a collection channel, i.e. /tacos, and a member channel, i.e. /tacos/1, for direct access.

If the model belongs_to other models, nested channels are created for all parents, grand parents, etc recursively



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/entangled/model.rb', line 83

def channels(tail = '')
  channels = []
  plural_name = self.class.name.underscore.pluralize

  # Add collection channel for child only. If the tails
  # is not empty, the function is being called recursively
  # for one of the parents, for which only member channels
  # are needed
  if tail.empty?
    collection_channel = "/#{plural_name}" + tail
    channels << collection_channel
  end

  # Add member channel
  member_channel = "/#{plural_name}/#{to_param}" + tail
  channels << member_channel

  # Add nested channels for each parent
  parents.each do |parent|
    # Only recusively add collection channel
    # for child
    if tail.empty?
      channels << parent.channels(collection_channel)
    end

    channels << parent.channels(member_channel)
  end

  channels.flatten
end