noodall-core 0.7.5 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm: 1.9.2
2
+ env: DB=mongodb
@@ -1,45 +1,41 @@
1
1
  module Noodall
2
2
  class Component
3
3
  include MongoMapper::EmbeddedDocument
4
-
4
+
5
5
  key :_type, String
6
6
  key :style, String
7
-
7
+
8
8
  embedded_in :node
9
-
9
+
10
10
  module ClassMethods
11
11
  def possible_slots
12
12
  Node.possible_slots
13
13
  end
14
14
 
15
15
  def allowed_positions(*args)
16
- @allowed_positions = args.reject{|a| !Node.possible_slots.include?(a) }.uniq
17
- end
18
-
19
- def positions
20
- @allowed_positions || []
16
+ warn "[DEPRECATION] `allowed_positions` is deprecated. Please use `Noodall::Node.slot` instead."
17
+ allowed_positions = args.reject{|a| !Node.possible_slots.include?(a) }.uniq
18
+
19
+ allowed_positions.each do |p|
20
+ Node.send("#{p}_slot_components") << self
21
+ end
21
22
  end
22
-
23
+
23
24
  def positions_classes(position)
24
- classes = []
25
- ObjectSpace.each_object(Class) do |c|
26
- next unless c.ancestors.include?(Component) and (c != Component) and c.positions.include?(position)
27
- classes << c
28
- end
29
- classes
25
+ Node.send("#{position}_slot_components")
30
26
  end
31
-
27
+
32
28
  def positions_names(position)
33
29
  positions_classes(position).collect{|c| c.name.titleize }
34
30
  end
35
-
31
+
36
32
  # Allow us to set the component to nil if we get a blank
37
33
  def to_mongo(value)
38
34
  return nil if value.blank?
39
35
  super
40
36
  end
41
-
37
+
42
38
  end
43
39
  extend ClassMethods
44
- end
40
+ end
45
41
  end
@@ -1,5 +1,5 @@
1
1
  module Noodall
2
2
  module Core
3
- VERSION = "0.7.5"
3
+ VERSION = "0.8.0"
4
4
  end
5
5
  end
data/lib/noodall/node.rb CHANGED
@@ -317,6 +317,8 @@ module Noodall
317
317
  class << self
318
318
  @@slots = []
319
319
 
320
+ # <b>DEPRECATED:</b> Please use <tt>slot</tt> instead.
321
+ #
320
322
  # Set the names of the slots that will be avaiable to fill with components
321
323
  # For each name new methods will be created;
322
324
  #
@@ -325,21 +327,56 @@ module Noodall
325
327
  # <name>_slots_count(count)
326
328
  # Reads back the count you set
327
329
  def slots(*args)
328
- @@slots = args.map(&:to_sym).uniq
330
+ warn "[DEPRECATION] `slots` is deprecated. Please use `slot` instead."
331
+ slots = args.map(&:to_sym).uniq
332
+
333
+ slots.each do |s|
334
+ slot(s)
335
+ end
336
+ end
329
337
 
330
- @@slots.each do |slot|
331
- puts "Noodall::Node Defined slot: #{slot}"
332
- define_singleton_method("#{slot}_slots") do |count|
333
- instance_variable_set("@#{slot}_slots_count", count)
338
+ # Define a slot type and what components are allowed to be place in that
339
+ # slot type.
340
+ #
341
+ # Generates methods in Noodall::Node models that allow you to set and read the
342
+ # number of slots of the name defined
343
+ #
344
+ # Noodall::Node.slot :small, Gallery, Picture
345
+ #
346
+ # class NicePage < Noodall::Node
347
+ # small_slots 3
348
+ # end
349
+ #
350
+ # NicePage.small_slots_count # => 3
351
+ #
352
+ # n = NicePage.new
353
+ # n.small_slot_0 = Gallery.new(...)
354
+ #
355
+ def slot(slot_name, *allowed_components)
356
+ if @@slots.include?(slot_name.to_sym)
357
+ warn "[WARNING] Overriding slot definition"
358
+ else
359
+ @@slots << slot_name.to_sym
360
+ puts "Noodall::Node Defined slot: #{slot_name}"
361
+ define_singleton_method("#{slot_name}_slots") do |count|
362
+ instance_variable_set("@#{slot_name}_slots_count", count)
334
363
  count.times do |i|
335
- slot_sym = "#{slot}_slot_#{i}".to_sym
364
+ slot_sym = "#{slot_name}_slot_#{i}".to_sym
336
365
  key slot_sym, Noodall::Component
337
- validates slot_sym, :slot => { :slot_type => slot }
366
+ validates slot_sym, :slot => { :slot_type => slot_name }
338
367
  validates_associated slot_sym
339
368
  end
340
369
  end
341
- define_singleton_method("#{slot}_slots_count") { instance_variable_get("@#{slot}_slots_count") }
370
+
371
+ define_singleton_method("#{slot_name}_slot_components") do
372
+ class_variable_get "@@#{slot_name}_slot_components".to_sym
373
+ end
374
+
375
+ define_singleton_method("#{slot_name}_slots_count") do
376
+ instance_variable_get("@#{slot_name}_slots_count")
377
+ end
342
378
  end
379
+ class_variable_set "@@#{slot_name}_slot_components".to_sym, allowed_components
343
380
  end
344
381
 
345
382
  def slots_count
@@ -365,49 +402,60 @@ module Noodall
365
402
  @template_classes || []
366
403
  end
367
404
 
368
- def root_templates
369
- return @root_templates if @root_templates
370
- classes = []
371
- ObjectSpace.each_object(Class) do |c|
372
- next unless c.ancestors.include?(Noodall::Node) and (c != Noodall::Node) and c.root_template?
373
- classes << c
374
- end
375
- @root_templates = classes
376
- end
377
-
378
405
  def template_names
379
- template_classes.collect{|c| c.name.titleize}.sort
406
+ template_classes.map{|c| c.name.titleize }.sort
380
407
  end
381
408
 
409
+ # Returns a lst of all node template classes available in
410
+ # in the tree
382
411
  def all_template_classes
383
412
  templates = []
384
- template_classes.each do |template|
413
+ root_templates.each do |template|
385
414
  templates << template
386
415
  templates = templates + template.template_classes
387
416
  end
388
- templates.uniq.collect{ |c| c.name.titleize }.sort
417
+ templates.uniq
389
418
  end
390
419
 
420
+ def all_template_names
421
+ all_template_classes.map{|c| c.name.titleize }.sort
422
+ end
423
+
424
+ # Set the Node templates that can be a child of this templates
425
+ # in the tree
391
426
  def sub_templates(*arr)
392
427
  @template_classes = arr
393
428
  end
394
429
 
430
+ @@root_templates = []
431
+
432
+ # Set the Node templates that can be a root of a tree
433
+ #
434
+ # Noodall::Node.root_templates Home, LandingPage
435
+ #
436
+ # Returns a list of the root templates
437
+ #
438
+ # Noodall::Node.root_templates # => [Home, LandingPage]
439
+ def root_templates(*templates)
440
+ @@root_templates = templates unless templates.empty?
441
+ @@root_templates
442
+ end
443
+
444
+ # <b>DEPRECATED:</b> Please use <tt>root_templates/tt> instead.
395
445
  def root_template!
396
- @root_template = true
446
+ warn "[DEPRECATION] `root_template` is deprecated. Please use `root_templates` instead."
447
+ @@root_templates << self
397
448
  end
398
449
 
399
450
  def root_template?
400
- @root_template
451
+ @@root_templates.include?(self)
401
452
  end
402
453
 
403
454
  # Returns a list of classes that can have this model as a child
404
455
  def parent_classes
405
- classes = []
406
- ObjectSpace.each_object(Class) do |c|
407
- next unless c.ancestors.include?(Noodall::Node) and (c != Noodall::Node) and c.template_classes.include?(self)
408
- classes << c
456
+ all_template_classes.find_all do |c|
457
+ c.template_classes.include?(self)
409
458
  end
410
- classes
411
459
  end
412
460
 
413
461
  # If rails style time zones are unavaiable fallback to standard now
@@ -418,7 +466,9 @@ module Noodall
418
466
 
419
467
  class SlotValidator < ActiveModel::EachValidator
420
468
  def validate_each(record, attribute, value)
421
- record.errors[attribute] << "cannnot contain #{value.class.name.humanize}" unless value.nil? or Noodall::Component.positions_classes(options[:slot_type]).include?(value.class)
469
+ unless value.nil? or Noodall::Component.positions_classes(options[:slot_type]).one?{|c| c.name == value._type }
470
+ record.errors[attribute] << "cannnot contain a #{value.class.name.humanize} component"
471
+ end
422
472
  end
423
473
  end
424
474
 
@@ -2,46 +2,74 @@ require 'spec_helper'
2
2
 
3
3
  describe Noodall::Component do
4
4
 
5
- it "should allow you to define the slots that are available" do
6
- Noodall::Node.slots :wide, :small, :main
5
+ it "should list components classes avaiable to a slot (deprecated)" do
6
+ class DListedComponent < Noodall::Component
7
+ allowed_positions :dsmall, :dwide
8
+ end
7
9
 
8
- Noodall::Component.possible_slots.should == [:wide, :small, :main]
10
+ Noodall::Node.dsmall_slot_components.should include(DListedComponent)
11
+ Noodall::Node.dmain_slot_components.should_not include(DListedComponent)
12
+ end
9
13
 
10
- class Promo < Noodall::Component
11
- allowed_positions :small, :wide, :main, :egg, :nog
14
+ it "should list components classes avaiable to a slot" do
15
+ class ListedComponent < Noodall::Component
12
16
  end
13
17
 
14
- Promo.positions.should have(3).things
18
+ Noodall::Node.slot :small, ListedComponent, Content
19
+ Noodall::Node.slot :main, Content
20
+
21
+ Noodall::Node.small_slot_components.should include(ListedComponent)
22
+ Noodall::Node.main_slot_components.should_not include(ListedComponent)
15
23
  end
16
24
 
17
- it "should list components classes avaiable to a slot" do
25
+ it "should be validated by the node (deprecated)" do
26
+ Noodall::Node.slots :wide, :small, :main
18
27
 
19
- class Promo < Noodall::Component
28
+ class DValidatedComponent < Noodall::Component
20
29
  allowed_positions :small, :wide
21
30
  end
22
31
 
23
- Promo.positions.should have(2).things
32
+ class DValidatedNode < Noodall::Node
33
+ main_slots 3
34
+ end
35
+
36
+ node = DValidatedNode.new :title => "Slot Node"
37
+ node.main_slot_0 = DValidatedComponent.new
38
+
39
+ node.save
24
40
 
25
- Noodall::Component.positions_classes(:small).should include(Promo)
26
- Noodall::Component.positions_classes(:main).should_not include(Promo)
41
+ node.errors.should have(1).things
27
42
  end
28
43
 
29
44
  it "should be validated by the node" do
30
- class Promo < Noodall::Component
31
- allowed_positions :small, :wide
45
+ class ValidatedComponent < Noodall::Component
46
+ end
47
+
48
+ Noodall::Node.slot :middle, ValidatedComponent
49
+ Noodall::Node.slot :main, Content
50
+
51
+ class ValidatedNode < Noodall::Node
52
+ main_slots 1
32
53
  end
33
54
 
34
- node = Factory(:page)
35
- node.main_slot_0 = Promo.new()
36
-
55
+ node = ValidatedNode.new :title => "Slot Node"
56
+ node.main_slot_0 = ValidatedComponent.new
57
+
37
58
  node.save
38
-
59
+
39
60
  node.errors.should have(1).things
40
61
  end
41
62
 
42
63
  it "should know it's node" do
43
- node = Factory(:page)
44
- node.small_slot_0 = Factory(:content)
64
+ Noodall::Node.slot :small, Content
65
+
66
+ class KnowingNode < Noodall::Node
67
+ small_slots 3
68
+ end
69
+
70
+ node = KnowingNode.new :title => "Slot Node"
71
+
72
+ node.small_slot_0 = Factory(:content)
45
73
 
46
74
  node.save!
47
75
 
@@ -3,8 +3,6 @@ class Content < Noodall::Component
3
3
  key :title, String
4
4
  key :url, String
5
5
  key :url_text, String
6
-
7
- allowed_positions :small, :wide
8
6
  end
9
7
 
10
8
  # And a factory to build it
@@ -3,10 +3,6 @@ class Page < Noodall::Node
3
3
  #sub_templates PageA, PageB, ArticlesList, LandingPage, EventsList
4
4
  searchable_keys :title, :description, :keywords, :body
5
5
  root_template!
6
-
7
- main_slots 1
8
- small_slots 4
9
- wide_slots 3
10
6
  end
11
7
 
12
8
  class LandingPage < Noodall::Node
data/spec/node_spec.rb CHANGED
@@ -7,6 +7,76 @@ describe Noodall::Node do
7
7
  }
8
8
  end
9
9
 
10
+ it "should allow me to set up slots (deprecated)" do
11
+ Noodall::Node.slots :dmain, :dsmall
12
+ class DSlotNode < Noodall::Node
13
+ dsmall_slots 3
14
+ dmain_slots 3
15
+ end
16
+
17
+ DSlotNode.new.slots.should be_instance_of(Array)
18
+ DSlotNode.slots_count.should == 6
19
+ end
20
+
21
+ it "should allow me to set up slots" do
22
+ Noodall::Node.slot :main, Content
23
+ Noodall::Node.slot :small, Content
24
+
25
+ class SlotNode < Noodall::Node
26
+ small_slots 3
27
+ main_slots 3
28
+ end
29
+
30
+ SlotNode.new.slots.should be_instance_of(Array)
31
+ SlotNode.small_slot_components.should include(Content)
32
+ SlotNode.slots_count.should == 6
33
+ end
34
+
35
+ it "should be able to list all slots (deprecated)" do
36
+ Noodall::Node.slots :dsmall
37
+
38
+ class DListSlotNode < Noodall::Node
39
+ dsmall_slots 3
40
+ end
41
+
42
+ class DListSlotComponent < Noodall::Component
43
+ allowed_positions :dsmall
44
+ end
45
+
46
+ node = DListSlotNode.new(:title => "Slot Node")
47
+ node.dsmall_slot_0 = DListSlotComponent.new(:body => "Some text")
48
+ node.dsmall_slot_1 = DListSlotComponent.new(:body => "Some more text")
49
+
50
+ node.save!
51
+
52
+ node.slots.should have(2).things
53
+
54
+ node.slots.first.body.should == "Some text"
55
+ node.slots.last.body.should == "Some more text"
56
+ end
57
+
58
+ it "should be able to list all slots" do
59
+ class ListSlotComponent < Noodall::Component
60
+ end
61
+
62
+ Noodall::Node.slot :small, ListSlotComponent
63
+
64
+ class ListSlotNode < Noodall::Node
65
+ small_slots 3
66
+ end
67
+
68
+ node = ListSlotNode.new(:title => "Slot Node")
69
+ node.small_slot_0 = ListSlotComponent.new(:body => "Some text")
70
+ node.small_slot_1 = ListSlotComponent.new(:body => "Some more text")
71
+
72
+ node.save!
73
+
74
+ node.slots.should have(2).things
75
+
76
+ node.slots.first.body.should == "Some text"
77
+ node.slots.last.body.should == "Some more text"
78
+ end
79
+
10
80
  it "should create a new instance given valid attributes" do
11
81
  Noodall::Node.create!(@valid_attributes)
12
82
  end
@@ -16,12 +86,21 @@ describe Noodall::Node do
16
86
  c.valid?.should == false
17
87
  end
18
88
 
19
- it "should know it's root templates" do
20
- class LandingPage < Noodall::Node
89
+ it "should know it's root templates (deprecated)" do
90
+ class DRootPage < Noodall::Node
21
91
  root_template!
22
92
  end
23
93
 
24
- Noodall::Node.template_classes.should include(LandingPage)
94
+ Noodall::Node.template_classes.should include(DRootPage)
95
+ end
96
+
97
+ it "should allow you to set the root templates" do
98
+ class RootPage < Noodall::Node
99
+ end
100
+
101
+ Noodall::Node.root_templates RootPage
102
+
103
+ Noodall::Node.template_classes.should include(RootPage)
25
104
  end
26
105
 
27
106
  it "should filter roots with options" do
@@ -41,7 +120,6 @@ describe Noodall::Node do
41
120
  node.class.should == Page
42
121
 
43
122
  class LandingPage < Noodall::Node
44
- root_template!
45
123
  end
46
124
 
47
125
  LandingPage.create!(@valid_attributes)
@@ -55,21 +133,13 @@ describe Noodall::Node do
55
133
  Noodall::Node.find_by_permalink('my-page').should == page
56
134
  end
57
135
 
58
- it "should allow you to set the number of slots" do
59
- class NicePage < Noodall::Node
60
- wide_slots 3
61
- small_slots 5
62
- end
63
-
64
- NicePage.slots_count.should == 8
65
- end
66
-
67
136
  describe "within a tree" do
68
137
  before(:each) do
69
138
  class LandingPage < Noodall::Node
70
- root_template!
71
139
  end
72
140
 
141
+ Noodall::Node.root_templates LandingPage
142
+
73
143
  @root = LandingPage.create!(:title => "Root")
74
144
 
75
145
  @child = Page.create!(:title => "Ickle Kid", :parent => @root)
@@ -194,23 +264,6 @@ describe Noodall::Node do
194
264
  p = Factory(:page)
195
265
  end
196
266
 
197
- it "should be able to list all slots" do
198
- ObjectSpace.each_object(Class) do |c|
199
- next unless c.ancestors.include?(Noodall::Node) and (c != Noodall::Node)
200
- c.new.slots.should be_instance_of(Array)
201
- end
202
-
203
- node = Factory(:page)
204
- node.small_slot_0 = Content.new(:body => "Some text")
205
- node.small_slot_1 = Content.new(:body => "Some more text")
206
-
207
- node.save!
208
-
209
- node.slots.should have(2).things
210
-
211
- node.slots.first.body.should == "Some text"
212
- node.slots.last.body.should == "Some more text"
213
- end
214
267
 
215
268
  it "should use a tree structure" do
216
269
  root = Page.create!(@valid_attributes)
@@ -398,13 +451,14 @@ describe Noodall::Node do
398
451
  sub_templates LandingPage
399
452
  end
400
453
 
454
+ Noodall::Node.root_templates LandingPage, ArticlesList
455
+
401
456
  Page.parent_classes.should include(LandingPage)
402
457
  Page.parent_classes.should_not include(ArticlesList)
403
458
  end
404
459
 
405
460
  it "should know what sub templates are allowed" do
406
461
  class LandingPage < Noodall::Node
407
- root_template!
408
462
  sub_templates Page, LandingPage
409
463
  end
410
464
  class Article < Noodall::Node
@@ -450,7 +504,6 @@ describe Noodall::Node do
450
504
 
451
505
  before(:each) do
452
506
  class LandingPage < Noodall::Node
453
- root_template!
454
507
  sub_templates Page, LandingPage
455
508
  end
456
509
  class Article < Noodall::Node
data/spec/spec_helper.rb CHANGED
@@ -12,9 +12,6 @@ MongoMapper.database = 'noodal-core-test'
12
12
  DatabaseCleaner.strategy = :truncation
13
13
  DatabaseCleaner.clean_with(:truncation)
14
14
 
15
-
16
- Noodall::Node.slots :main, :wide, :small
17
-
18
15
  require 'factories/node'
19
16
  require 'factories/component'
20
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noodall-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-26 00:00:00.000000000Z
12
+ date: 2011-10-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo_mapper
16
- requirement: &14024980 !ruby/object:Gem::Requirement
16
+ requirement: &3394060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.9.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14024980
24
+ version_requirements: *3394060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ramdiv-mongo_mapper_acts_as_tree
27
- requirement: &14023780 !ruby/object:Gem::Requirement
27
+ requirement: &3393140 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.1.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *14023780
35
+ version_requirements: *3393140
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: mm-multi-parameter-attributes
38
- requirement: &14022840 !ruby/object:Gem::Requirement
38
+ requirement: &3392240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.2.1
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *14022840
46
+ version_requirements: *3392240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: canable
49
- requirement: &14022000 !ruby/object:Gem::Requirement
49
+ requirement: &3391320 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.1.1
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *14022000
57
+ version_requirements: *3391320
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mm-versionable
60
- requirement: &14021120 !ruby/object:Gem::Requirement
60
+ requirement: &3388620 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.2.5
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *14021120
68
+ version_requirements: *3388620
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: ruby-stemmer
71
- requirement: &14020320 !ruby/object:Gem::Requirement
71
+ requirement: &3387460 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *14020320
79
+ version_requirements: *3387460
80
80
  description: Core data objects for Noodall
81
81
  email:
82
82
  - steve@wearebeef.co.uk
@@ -87,6 +87,7 @@ files:
87
87
  - .document
88
88
  - .gitignore
89
89
  - .rvmrc
90
+ - .travis.yml
90
91
  - Gemfile
91
92
  - LICENSE
92
93
  - README.rdoc
@@ -122,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
123
  version: '0'
123
124
  segments:
124
125
  - 0
125
- hash: -1321159501201934569
126
+ hash: 1553541379450765350
126
127
  required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  none: false
128
129
  requirements: