Module: Taglish::Core

Defined in:
lib/taglish/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
# File 'lib/taglish/core.rb', line 3

def self.included(base)
  base.class_eval do
    after_save :save_tags
  end
end

Instance Method Details

#add_tag_on(context, tag) ⇒ Object



66
67
# File 'lib/taglish/core.rb', line 66

def add_tag_on(context, tag)
end

#all_tags_list_on(context) ⇒ Object



62
63
64
# File 'lib/taglish/core.rb', line 62

def all_tags_list_on(context)
  raise "TODO LATER"
end

#mark_tag_list_as_changed(tag_type, new_list) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/taglish/core.rb', line 43

def mark_tag_list_as_changed(tag_type, new_list)
  value = new_list.is_a?(Array) ? new_list.join(', ') : new_list
  attrib = tag_list_attribute_name_for(tag_type)

  old = changed_attributes[attrib]
  if old.nil?
    old = tag_list_on(tag_type).to_s
    if old.to_s != value.to_s
      changed_attributes[attrib] = old
      tag_list_changed!(tag_type)
    end
  else
    if old.to_s == value.to_s
      changed_attributes.delete(attrib)
      tag_list_changed!(tag_type, false)
    end
  end
end

#reload(*args) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/taglish/core.rb', line 91

def reload(*args)
  self.class.tag_types.each do |tt_name, tt|
    instance_variable_set(tag_list_variable_name_for(tt), nil)
    tag_list_changed!(tt, false)
  end
 
  super(*args)
end

#save_tagsObject



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
# File 'lib/taglish/core.rb', line 100

def save_tags
  self.class.tag_types.each do |tag_type_name, tag_type|
    # next unless changed_attributes[tag_list_attribute_name_for(tag_type)]
    next unless tag_list_changed?(tag_type)

    new_tag_list = instance_variable_get(tag_list_variable_name_for(tag_type))

    # Tag objects for the new set of taggings:
    # tags = tag_type.find_or_create_tags(*new_tags)

    # Tagging objects for the new set of taggings (not persisted):
    # new_taggings = Hash[new_tag_list.to_tagging_array.map{|tg| [tg.name, tg]}]
    new_taggings = new_tag_list.to_tagging_array

    # Tagging objects for the previous set of taggings:
    current_taggings = taggings_by_name(tag_type_name)

    if tag_type.ordered?
      raise "TODO: Ordering not supported yet"
    else
      new_taggings.each do |tg|
        old_tg = current_taggings[tg.name]
        if old_tg
          if old_tg.score != tg.score
            old_tg.update_attribute(:score, tg.score)
          end
          current_taggings.delete tg.name
        else
          tg.taggable = self
          tg.tag = Taglish::Tag.find_or_create_by_name(tg.name)
          tg.context = tag_type_name
          tg.save!
        end
      end
    end

    # Remove unused taggings:
    Taglish::Tagging.destroy_all :id => current_taggings.values.map(&:id)
  end

  true
end

#set_tag_list_on(tag_type, new_list) ⇒ Object



33
34
35
36
37
# File 'lib/taglish/core.rb', line 33

def set_tag_list_on(tag_type, new_list)
  n = tag_list_variable_name_for(tag_type)
  mark_tag_list_as_changed(tag_type, new_list)
  instance_variable_set(n, Taglish::TagList.from(tag_type, new_list))
end

#tag_list_changed!(tag_type, changed = true) ⇒ Object



87
88
89
# File 'lib/taglish/core.rb', line 87

def tag_list_changed!(tag_type, changed=true)
  instance_variable_set(dirty_tag_list_variable_name_for(tag_type), changed)
end

#tag_list_changed?(tag_type) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/taglish/core.rb', line 83

def tag_list_changed?(tag_type)
  instance_variable_get(dirty_tag_list_variable_name_for(tag_type))
end

#tag_list_on(tag_type) ⇒ Object

Returns an array of strings



26
27
28
29
30
31
# File 'lib/taglish/core.rb', line 26

def tag_list_on(tag_type)
  n = tag_list_variable_name_for(tag_type)
  instance_variable_get(n) ||
    instance_variable_set(n,
      Taglish::TagList.new(tag_type, *taggings_on(tag_type.name).map(&:to_s)))
end

#taggable?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/taglish/core.rb', line 79

def taggable?
  self.class.taggable?
end

#taggings_by_name(context) ⇒ Object



39
40
41
# File 'lib/taglish/core.rb', line 39

def taggings_by_name(context)
  Hash[taggings_on(context).map{|tg| [tg.name, tg]}]
end

#taggings_on(context) ⇒ Object

Returns an array of Taggings context should be plural



17
18
19
20
21
22
23
# File 'lib/taglish/core.rb', line 17

def taggings_on(context)
  q = taggings.includes(:tag).where(%Q{
               #{Taglish::Tagging.table_name}.context = ?
           AND #{Taglish::Tagging.table_name}.tagger_id IS NULL}, context.to_s)
  q = q.order("#{Taglish::Tagging.table_name}.id") if tags_have_order?(context)
  q
end

#tags_have_order?(context) ⇒ Boolean

context is plural

Returns:

  • (Boolean)


75
76
77
# File 'lib/taglish/core.rb', line 75

def tags_have_order?(context)
  tag_types[context].ordered?
end

#tags_have_score?(context) ⇒ Boolean

context is plural

Returns:

  • (Boolean)


70
71
72
# File 'lib/taglish/core.rb', line 70

def tags_have_score?(context)
  tag_types[context].scored?
end

#tags_on(context) ⇒ Object

Returns an array of Tags context should be plural



11
12
13
# File 'lib/taglish/core.rb', line 11

def tags_on(context)
  taggings.on(context).map{|tg| tg.tag}
end