labelized 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.3
1
+ 0.7.0
data/labelized.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{labelized}
8
- s.version = "0.6.3"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Peter T. Brown"]
12
- s.date = %q{2011-08-16}
12
+ s.date = %q{2011-11-15}
13
13
  s.description = %q{A better tag library}
14
14
  s.email = %q{peter@pathable.com}
15
15
  s.extra_rdoc_files = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "labelized.gemspec",
30
30
  "lib/labelized.rb",
31
31
  "lib/labelized/label_concern.rb",
32
+ "lib/labelized/label_concern.rb.orig",
32
33
  "lib/labelized/label_list.rb",
33
34
  "lib/labelized/label_set_concern.rb",
34
35
  "lib/labelized/labeling_concern.rb",
@@ -1,3 +1,5 @@
1
+ require 'labelized/label_list'
2
+
1
3
  module Labelized
2
4
  module LabelConcern
3
5
  extend ActiveSupport::Concern
@@ -10,33 +12,23 @@ module Labelized
10
12
  end
11
13
 
12
14
  module ClassMethods
13
- def labelized(params)
15
+ def labelized(params = {})
14
16
  setup_labelized params
15
17
 
16
- def find_or_build_by_list(labels, labeled, label_set_name = nil)
18
+ def find_or_build_by_list(labels, labeled, label_set_name = nil)
17
19
  label_set_class = (labelized_options[:label_set_class_name] || 'LabelSet').constantize
18
- existing_labels = self.label_scope(labeled).where(:name => labels)
19
20
 
20
21
  unless label_set_name.blank?
21
22
  label_set_name.strip! # ooh lah lah
22
23
  label_set = label_set_class.label_scope(labeled).find_or_initialize_by_name(label_set_name)
23
24
  end
24
25
 
25
- new_label_names = labels.reject do |name|
26
- existing_labels.any? do |label|
27
- name.downcase == label.name.downcase
28
- end
29
- end
30
-
31
- new_labels = new_label_names.map do |name|
32
- name.strip!
33
- self.label_scope(labeled).build(:name => name, :label_set => label_set)
26
+ LabelList.from(labels).map do |label|
27
+ self.label_scope(labeled).find_or_initialize_by_name(label.strip, :label_set => label_set)
34
28
  end
35
-
36
- existing_labels + new_labels
37
29
  end
38
30
 
39
31
  end
40
32
  end
41
33
  end
42
- end
34
+ end
@@ -0,0 +1,42 @@
1
+ module Labelized
2
+ module LabelConcern
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :labelings, :dependent => :destroy
7
+ belongs_to :label_set
8
+
9
+ extend Support
10
+ end
11
+
12
+ module ClassMethods
13
+ def labelized(params = {})
14
+ setup_labelized params
15
+
16
+ def find_or_build_by_list(labels, labeled, label_set_name = nil)
17
+ label_set_class = (labelized_options[:label_set_class_name] || 'LabelSet').constantize
18
+ existing_labels = self.label_scope(labeled).where(:name => labels)
19
+
20
+ unless label_set_name.blank?
21
+ label_set_name.strip! # ooh lah lah
22
+ label_set = label_set_class.label_scope(labeled).find_or_initialize_by_name(label_set_name)
23
+ end
24
+
25
+ new_label_names = labels.reject do |name|
26
+ existing_labels.any? do |label|
27
+ name.downcase == label.name.downcase
28
+ end
29
+ end
30
+
31
+ new_labels = new_label_names.map do |name|
32
+ name.strip!
33
+ self.label_scope(labeled).build(:name => name, :label_set => label_set)
34
+ end
35
+
36
+ existing_labels + new_labels
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/class/attribute_accessors'
2
+
1
3
  module Labelized
2
4
  class LabelList < Array
3
5
  cattr_accessor :delimiter
@@ -93,4 +95,4 @@ module Labelized
93
95
  args.flatten!
94
96
  end
95
97
  end
96
- end
98
+ end
@@ -44,6 +44,8 @@ module Labelized
44
44
  labels = [labels].flatten
45
45
  return [] if labels.empty?
46
46
 
47
+ label_class.is_labelized? rescue raise "label_class #{label_class} must be labelized"
48
+
47
49
  self.labels = cache_label_set(label_set_name, label_class.label_scope(self).find_or_build_by_list(labels, self, label_set_name.to_s))
48
50
  end
49
51
 
@@ -53,13 +55,17 @@ module Labelized
53
55
  cache_label_get(label_set_name) || self.labels.where(:label_set_id => label_set.id)
54
56
  end
55
57
 
58
+ # Convenience setter to the label_set name. Accepts a single item or an array.
56
59
  labelized_label_set_names.map(&:to_s).each do |label_set_name|
57
60
  define_method "#{label_set_name}=" do |labels|
58
61
  label labels, label_set_name
59
62
  end
60
-
63
+
64
+ # Convenience acccessor to the label_set name. If singular will return a single label.
65
+ # Otherwise returns an array of labels.
61
66
  define_method "#{label_set_name}" do
62
- label_for label_set_name
67
+ labels = label_for label_set_name
68
+ Labelized::Support.singular?(label_set_name) ? labels[0] : labels
63
69
  end
64
70
  end
65
71
  end
@@ -23,6 +23,7 @@ module Labelized
23
23
  end
24
24
 
25
25
  def setup_label_scope(scopes)
26
+ return if scopes.blank?
26
27
  self.class_eval do
27
28
  scope :label_scope, lambda {|labeled| where(*scopes.collect{|s| {s => labeled.send(s)}}.flatten)}
28
29
  end
@@ -38,5 +39,8 @@ module Labelized
38
39
  class_inheritable_reader(:labelized_label_set_names)
39
40
  end
40
41
 
42
+ def is_labelized?
43
+ true
44
+ end
41
45
  end
42
46
  end
@@ -4,7 +4,7 @@ class Label < ActiveRecord::Base
4
4
  include Labelized::LabelConcern
5
5
 
6
6
  belongs_to :root
7
- labelized :scope => :root_id
7
+ labelized
8
8
  end
9
9
 
10
10
  class Root < ActiveRecord::Base
@@ -29,6 +29,13 @@ class Thing < ActiveRecord::Base
29
29
  labelized :tags
30
30
  end
31
31
 
32
+ class InvalidThing < Thing
33
+ include Labelized::LabelizedConcern
34
+ belongs_to :root
35
+ labelized :tags, :label_class_name => 'Root'
36
+ end
37
+
38
+
32
39
  # thing.tags # [<label id=1>]
33
40
  # label.label_set
34
41
  # thing.labels # [<label id=1>]
@@ -72,6 +79,7 @@ describe Labelized::LabelizedConcern do
72
79
  it{should be_instance_of Array}
73
80
  it{should include('tag one')}
74
81
  its(:size){should == 1}
82
+
75
83
  end
76
84
 
77
85
  context 'after saved' do
@@ -100,4 +108,9 @@ describe Labelized::LabelizedConcern do
100
108
  end
101
109
  end
102
110
 
111
+ context 'when invalid' do
112
+ let(:invalid_thing){InvalidThing.new()}
113
+
114
+ specify{expect{invalid_thing.label'tag', :tags}.to raise_exception}
115
+ end
103
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: labelized
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-16 00:00:00.000000000 -07:00
12
+ date: 2011-11-15 00:00:00.000000000 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
- requirement: &2159282580 !ruby/object:Gem::Requirement
17
+ requirement: &2167182500 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2159282580
25
+ version_requirements: *2167182500
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: bundler
28
- requirement: &2159281900 !ruby/object:Gem::Requirement
28
+ requirement: &2167181900 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2159281900
36
+ version_requirements: *2167181900
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: jeweler
39
- requirement: &2159281340 !ruby/object:Gem::Requirement
39
+ requirement: &2167181300 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2159281340
47
+ version_requirements: *2167181300
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: guard
50
- requirement: &2159280860 !ruby/object:Gem::Requirement
50
+ requirement: &2167180700 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2159280860
58
+ version_requirements: *2167180700
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: growl
61
- requirement: &2159280360 !ruby/object:Gem::Requirement
61
+ requirement: &2167180100 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2159280360
69
+ version_requirements: *2167180100
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rcov
72
- requirement: &2159279880 !ruby/object:Gem::Requirement
72
+ requirement: &2167179500 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2159279880
80
+ version_requirements: *2167179500
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rspec
83
- requirement: &2159279380 !ruby/object:Gem::Requirement
83
+ requirement: &2167174100 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2159279380
91
+ version_requirements: *2167174100
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: sqlite3
94
- requirement: &2159278900 !ruby/object:Gem::Requirement
94
+ requirement: &2167173620 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *2159278900
102
+ version_requirements: *2167173620
103
103
  description: A better tag library
104
104
  email: peter@pathable.com
105
105
  executables: []
@@ -120,6 +120,7 @@ files:
120
120
  - labelized.gemspec
121
121
  - lib/labelized.rb
122
122
  - lib/labelized/label_concern.rb
123
+ - lib/labelized/label_concern.rb.orig
123
124
  - lib/labelized/label_list.rb
124
125
  - lib/labelized/label_set_concern.rb
125
126
  - lib/labelized/labeling_concern.rb
@@ -146,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
147
  version: '0'
147
148
  segments:
148
149
  - 0
149
- hash: 671687505935235281
150
+ hash: -2535347908215351964
150
151
  required_rubygems_version: !ruby/object:Gem::Requirement
151
152
  none: false
152
153
  requirements: