has_content 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,7 @@
1
+ = 0.0.3
2
+ * Set attr_accessible to :content only on the record, set name and owner via relationships
3
+
4
+ = 0.0.2
5
+
1
6
  = 0.0.1
2
7
  * Initial Release
@@ -41,22 +41,25 @@ module HasContent
41
41
 
42
42
  has_one "#{name}_content".to_sym, options.reverse_merge(:as => 'owner', :class_name => 'HasContent::Record', :dependent => :destroy, :conditions => {name: name}, :autosave => true)
43
43
 
44
+ # content getter (delegates to the content association)
44
45
  define_method name do
45
- send("find_or_build_#{name}_content").content
46
+ send("get_#{name}_content").content
46
47
  end
47
48
 
49
+ # content setter (delegates to the content association, updating timestamps on owner record if required)
48
50
  define_method "#{name}=" do |value|
49
- (send("find_or_build_#{name}_content").content = value).tap do |*|
50
- if respond_to?(:updated_at?) && send("find_or_build_#{name}_content").changed?
51
+ (send("get_#{name}_content").content = value).tap do |*|
52
+ if respond_to?(:updated_at?) && send("get_#{name}_content").changed?
51
53
  updated_at_will_change!
52
54
  end
53
55
  end
54
56
  end
55
57
 
56
- define_method "find_or_build_#{name}_content" do
57
- send("#{name}_content") || send("build_#{name}_content", name: name)
58
+ # find or build (and save if possible) the content association
59
+ define_method "get_#{name}_content" do
60
+ send("#{name}_content") or send("build_#{name}_content").tap {|r| r.name = name; r.save unless new_record? }
58
61
  end
59
- private "find_or_build_#{name}_content".to_sym
62
+ private "get_#{name}_content".to_sym
60
63
  end
61
64
  end
62
65
  end
@@ -1,5 +1,7 @@
1
1
  module HasContent
2
2
  class Record < ActiveRecord::Base
3
+ attr_accessible :content # :name, and :owner are set by content owners
4
+
3
5
  self.table_name = 'has_content_records'
4
6
 
5
7
  belongs_to :owner, polymorphic: true
@@ -8,17 +10,9 @@ module HasContent
8
10
  inclusion: {in: lambda(&:allowed_names), if: :owner},
9
11
  uniqueness: {scope: %w(owner_id owner_type), if: :owner_persisted?}
10
12
 
11
- before_create :verify_valid_owner! # this badboy is here because owner is sometimes not present at validation
13
+ before_create :verify_valid_owner! # this badboy is here because owner is sometimes not present at validation
12
14
  # because content is a has_one on owner, with autosave true
13
-
14
- # Contents are only ever instantiated by has_content assoc, and it's convenient for them to
15
- # always refer (for links to content etc).
16
- # If there is a validation problem, the save will fail silently (which is fine)
17
- def initialize(*)
18
- super
19
- save if new_record? && owner_persisted?
20
- end
21
-
15
+
22
16
  def to_s
23
17
  content
24
18
  end
@@ -1,3 +1,3 @@
1
1
  module HasContent
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -3,21 +3,16 @@ require 'spec_helper'
3
3
  describe HasContent::Record do
4
4
  subject { record }
5
5
 
6
- let(:record) { described_class.new attrs }
7
- let(:attrs) { {} }
8
-
9
- it "does not save itself - as it's invalid" do
10
- should be_new_record
11
- end
6
+ let(:record) { described_class.new }
12
7
 
13
8
  describe 'with valid attributes' do
14
- let(:attrs) { {:name => 'body', :owner => owner} }
15
- let(:owner) { ContentOwner.create! }
16
-
17
- it "saves itself (to enable always referring relationships)" do
18
- should_not be_new_record
9
+ before do
10
+ record.name = 'body'
11
+ record.owner = owner
19
12
  end
20
13
 
14
+ let(:owner) { ContentOwner.create! }
15
+
21
16
  it { should be_valid }
22
17
 
23
18
  describe '[validation]' do
@@ -28,7 +23,7 @@ describe HasContent::Record do
28
23
 
29
24
  it 'requires :name + :owner be unique' do
30
25
  record.save!
31
- record = described_class.new :name => 'body', :owner => owner
26
+ record = described_class.new {|r| r.name = 'body'; r.owner = owner }
32
27
  record.should_not be_valid
33
28
  record.name = 'excerpt'
34
29
  record.should be_valid
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_content
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70244228437940 !ruby/object:Gem::Requirement
16
+ requirement: &70179807906620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70244228437940
24
+ version_requirements: *70179807906620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70244228459060 !ruby/object:Gem::Requirement
27
+ requirement: &70179807716000 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70244228459060
35
+ version_requirements: *70179807716000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70244228456400 !ruby/object:Gem::Requirement
38
+ requirement: &70179807713540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70244228456400
46
+ version_requirements: *70179807713540
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sqlite3
49
- requirement: &70244228453880 !ruby/object:Gem::Requirement
49
+ requirement: &70179807710640 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70244228453880
57
+ version_requirements: *70179807710640
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: database_cleaner
60
- requirement: &70244228452440 !ruby/object:Gem::Requirement
60
+ requirement: &70179807709560 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,9 +65,9 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70244228452440
68
+ version_requirements: *70179807709560
69
69
  description: Simple wrapper for adding content via a polymorphic join for any active
70
- record. Version 0.0.2.
70
+ record. Version 0.0.3.
71
71
  email:
72
72
  - ian.w.white@gmail.com
73
73
  executables: []
@@ -105,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  segments:
107
107
  - 0
108
- hash: -2447319576099344314
108
+ hash: 778003700105874322
109
109
  required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  none: false
111
111
  requirements:
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  segments:
116
116
  - 0
117
- hash: -2447319576099344314
117
+ hash: 778003700105874322
118
118
  requirements: []
119
119
  rubyforge_project:
120
120
  rubygems_version: 1.8.15