Module: Polytag

Defined in:
lib/polytag.rb,
lib/polytag/version.rb,
lib/polytag/exceptions.rb,
lib/polytag/concerns/taggable.rb,
lib/polytag/concerns/tag_owner.rb,
lib/polytag/concerns/taggable/class_helpers.rb,
lib/polytag/concerns/taggable/model_helpers.rb,
lib/polytag/concerns/tag_owner/class_helpers.rb,
lib/polytag/concerns/tag_owner/model_helpers.rb,
lib/generators/polytag/install/install_generator.rb,
lib/polytag/concerns/taggable/association_extensions.rb,
lib/polytag/concerns/tag_owner/association_extensions.rb,
lib/polytag/concerns/tag_owner/association_extensions/owned_tags.rb

Overview

Polytag Module

Defined Under Namespace

Modules: Concerns, Generators Classes: CantFindPolytagModel, Connection, NotPolytagModel, NotTagOwner, NotTagOwnerOrTaggable, NotTaggable, Tag, TagGroup

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.get(type = :tag, foc = nil, data = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/polytag.rb', line 25

def get(type = :tag, foc = nil, data = {})
  force_name_search = false
  retried = false

  # Allow hashes to be passed
  if type.is_a?(Hash) || type.is_a?(ActiveRecord::Base)
    data = type
    type = nil
    foc  = nil
  elsif foc.is_a?(Hash) || foc.is_a?(ActiveRecord::Base)
    data = foc
    foc  = nil
  end

  # Reject nil or false data keys
  # also grab the foc value
  if data.is_a?(Hash)
    data = data.delete_if { |k, v| v.nil? || v == false }
    foc  = data.delete(:foc) if data.has_key?(:foc)
    data[:tag_group] = :default if data[:owner] && ! data[:tag_group]
  end

  # Ensure that we are processing the right data if the data comes in in a unexpected way
  if data.is_a?(Hash) && data.keys.size == 1 && [:tag, :tag_group].include?(data.keys.first)
    type = data.keys.first
    data = data[data.keys.first]
  end

  # Return the model if the model passed matches
  if data.is_a?(ActiveRecord::Base)
    return data if data.instance_of?(Tag) && (type ? type == :tag : true)
    return data if data.instance_of?(TagGroup) && (type ? type == :tag_group : true)
    return data if data.instance_of?(Connection) && (type ? type == :connection : true)
    raise NotAPolytagModel, "#{data.inspect} is not a Polytag model in the requested form."
  elsif data.is_a?(String) || data.is_a?(Symbol)
    data = "#{data}".strip
  end

  # Handle how the results are returned
  if data.is_a?(Hash) && data.keys.sort == [:owner, :tag, :tag_group, :tagged]

    # Get the tag owner
    tag_owner = get_tag_owner_or_taggable(:hash, data[:owner])

    # Get the tag group
    querydata = {owner: tag_owner, tag_group: data[:tag_group], foc: foc}
    tag_group = get(:tag_group, foc, querydata)

    # Get the tag
    querydata = {tag: data[:tag], tag_group: tag_group, foc: foc}
    tag       = get(:tag, foc, querydata)

    # Create the data we are using to create the connection
    querydata = tag_owner.merge foc: foc,
      polytag_tag_id: tag.id,
      polytag_tag_group_id: tag_group.id,
      tagged: data[:tagged]

    __connection_processor(querydata)
  elsif data.is_a?(Hash) && data.keys.sort == [:tag, :tag_group, :tagged]

    # Get the tag group
    querydata = {tag_group: data[:tag_group], foc: foc}
    tag_group = get(:tag_group, foc, querydata)

    # Get the tag
    querydata = {tag: data[:tag], tag_group: tag_group, foc: foc}
    tag       = get(:tag, foc, querydata)

    # Create the data we are using to create the connection
    querydata = {}.merge foc: foc,
      polytag_tag_group_id: tag_group.id,
      polytag_tag_id: tag.id,
      tagged: data[:tagged]

    __connection_processor(querydata)
  elsif data.is_a?(Hash) && data.keys.sort == [:tag, :tagged]

    # Get the tag
    querydata = {tag: data[:tag], foc: foc}
    tag = get(:tag, foc, querydata) do |ar|
      ar.where(polytag_tag_group_id: nil)
    end

    # If we expected a result and we don't have one raise
    raise CantFindPolytagModel if foc && ! tag

    # Create the data we are using to create the connection
    querydata = {}.merge foc: foc,
      polytag_tag_group_id: nil,
      polytag_tag_id: tag.id,
      tagged: data[:tagged]

    __connection_processor(querydata)
  elsif data.is_a?(Hash) && data.keys.sort == [:owner, :tag, :tag_group]

    # Get the tag owner
    tag_owner = get_tag_owner_or_taggable(data[:owner])

    # Get the tag group
    querydata = {owner: tag_owner, tag_group: data[:tag_group], foc: foc}
    tag_group = get(:tag_group, foc || :first, querydata)

    # Get the tag
    querydata = {tag: data[:tag], tag_group: tag_group, foc: foc}
    tag       = get(:tag, foc, querydata)

    return tag
  elsif data.is_a?(Hash) && data.keys.sort == [:owner, :tag_group]
    # Get the tag group with owner
    tag_owner = get_tag_owner_or_taggable(:hash, data[:owner])

    get(:tag_group, foc, data[:tag_group]) do |ar|
      ar.where(tag_owner)
    end
  elsif data.is_a?(Hash) && data.keys.sort == [:tag, :tag_group]
    # Get the tag with tag group
    tag_group = get(:tag_group, foc || :first, data[:tag_group])

    get(:tag, foc, data[:tag]) do |ar|
      ar.where(polytag_tag_group_id: tag_group.id)
    end
  elsif type && ! force_name_search && __numerical_string_id?(data)

    # Force foc to be a first if the option is requested
    foc = :first if foc

    result = const_get("#{type}".camelize).where(id: data.to_i)
    result = yield(result) if block_given?
    result = result.__send__(foc) if foc
    return result
  elsif type && (force_name_search || data.is_a?(String))

    result = const_get("#{type}".camelize).where(name: data)
    result = yield(result) if block_given?
    result = result.__send__(foc) if foc
    return result
  else
    raise CantFindPolytagModel, "Can't find a polytag model with #{data.inspect}."
  end
rescue ActiveRecord::RecordNotFound => e
  raise e if e.instance_of?(CantFindPolytagModel) || retried
  force_name_search = true
  retried = true
  foc = :first
  retry
end

.get_tag_owner_or_taggable(result = :object, data = {}) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/polytag.rb', line 182

def get_tag_owner_or_taggable(result = :object, data = {})

  # Ensure result is is always set
  if result.is_a?(Hash) || result.is_a?(ActiveRecord::Base)
    data   = result
    result = :object
  end

  if data.is_a?(Hash)
    data = if data.keys.sort == [:id, :type]
      data[:type].camelize.constantize.find(data[:id])
    elsif data.keys.sort == [:owner_id, :owner_type]
      data[:owner_type].camelize.constantize.find(data[:owner_id])
    elsif data.keys.sort == [:tagged_id, :tagged_type]
      data[:tagged_type].camelize.constantize.find(data[:tagged_id])
    end
  end

  # The class we need to test
  dclass = data.class

  # Ensure that the model we return is a taggable or tag owner
  if __inherits(dclass, ActiveRecord::Base, Concerns::TagOwner) || __inherits(dclass, ActiveRecord::Base, Concerns::Taggable)

    # Return hash options for the type
    if result == :hash
      if __inherits(dclass, ActiveRecord::Base, Concerns::TagOwner)
        return {owner_type: "#{dclass}", owner_id: data.id}
      elsif __inherits(dclass, ActiveRecord::Base, Concerns::Taggable)
        return {tagged_type: "#{dclass}", tagged_id: data.id}
      end
    end

    # Return the raw object
    return data if result == :object
  end

  # Raise if not a taggable or tag owner object
  raise NotTagOwnerOrTaggable, "The model #{data.inspect} is not a polytag tag owner or taggable model."
end

.tag_group_owner?(owner = {}, raise_on_error = false) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
# File 'lib/polytag.rb', line 173

def tag_group_owner?(owner = {}, raise_on_error = false)
  owner = get_polymorphic(owner)
  return true if __inherits(data.class, ActiveRecord::Base, Concerns::TagOwner)
  raise NotTagOwner, "This model #{owner.inspect} is not a polytag tag owner."
rescue NotTagOwner, NotTagOwnerOrTaggable => e
  raise e unless raise_on_error
  false
end