Method: Entangled::Model::InstanceMethods#channels

Defined in:
lib/entangled/model.rb

#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



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
113
114
115
116
117
118
119
# File 'lib/entangled/model.rb', line 86

def channels(tail = '')
  channels = []

  # If the record is new, it should not have any channels
  return channels if new_record?

  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