eav_presenter 0.0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/eav_presenter.rb +7 -7
  2. data/lib/test.rb +68 -0
  3. metadata +4 -5
@@ -17,6 +17,7 @@ module EavPresenter
17
17
  end
18
18
 
19
19
  def load_eav_attr_values_if_not_loaded
20
+ return [] if self.new_record?
20
21
  entity_foreign_key = self.class.instance_variable_get("@eav_entity_foreign_key")
21
22
  @eav_attr_values ||= self.class.instance_variable_get("@eav_value_class").send("find_all_by_#{entity_foreign_key}",self.id)
22
23
  end
@@ -24,12 +25,12 @@ module EavPresenter
24
25
  def save_eav_attr_values
25
26
  self.class.instance_variable_get("@eav_attrs").each { |at|
26
27
  eavs = load_eav_attr_values_if_not_loaded
27
- eav = eavs.find { |i| i.attrId == at.id } #eav in the db
28
28
  name = at.eav_attr_name
29
29
  entity_foreign_key = self.class.instance_variable_get("@eav_entity_foreign_key")
30
30
  attr_foreign_key = self.class.instance_variable_get("@eav_attr_foreign_key")
31
31
  eav_value_class = self.class.instance_variable_get("@eav_value_class")
32
- ivalue = send(name)
32
+ eav = eavs.find { |i| i.send(attr_foreign_key) == at.id } #eav in the db, can be missing
33
+ ivalue = send(name).to_s
33
34
  if attribute_changed?(name)
34
35
  if eav
35
36
  if ivalue.blank?
@@ -70,16 +71,16 @@ module EavPresenter
70
71
  if instance_variables.include?("@#{at.eav_attr_name}")
71
72
  instance_variable_get("@#{at.eav_attr_name}")
72
73
  else
73
- eavs.find { |i| i.attrId == at.id }.try(:value)
74
+ eavs.find { |i| i.send(self.class.instance_variable_get("@eav_attr_foreign_key")) == at.id }.try(:value)
74
75
  end
75
76
  }
76
77
 
77
78
  define_method("#{at.eav_attr_name}=") { |value|
78
- raise "EAV value for #{at.eav_attr_name} must be a string or nil" unless value.nil? || value.is_a?(String)
79
+ # raise "EAV value for #{at.eav_attr_name} must be a string or nil" unless value.nil? || value.is_a?(String)
79
80
  eavs = load_eav_attr_values_if_not_loaded
80
81
  instance_variable_set("@#{at.eav_attr_name}",value)
81
- old = eavs.find { |i| i.attrId == at.id }.try(:value)
82
- if value == old
82
+ old = eavs.find { |i| i.send(self.class.instance_variable_get("@eav_attr_foreign_key")) == at.id }.try(:value)
83
+ if value.to_s == old.to_s # to consider nil and blank as same
83
84
  changed_attributes.delete(at.eav_attr_name)
84
85
  else
85
86
  changed_attributes[at.eav_attr_name] = old
@@ -91,4 +92,3 @@ module EavPresenter
91
92
  end
92
93
  end
93
94
  end
94
-
@@ -0,0 +1,68 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'eav_presenter.rb'
4
+
5
+ ActiveRecord::Base.establish_connection({
6
+ :adapter => 'jdbcmysql',
7
+ :host => 'localhost',
8
+ :username => 'root',
9
+ :database => 'eav_test'
10
+ })
11
+
12
+ ActiveRecord::Migration.class_eval do
13
+ create_table :posts do |t|
14
+ t.integer :id
15
+ t.string :title
16
+ end
17
+
18
+ create_table :post_attributes do |t|
19
+ t.integer :id
20
+ t.string :name
21
+ end
22
+
23
+ create_table :post_attr_values do |t|
24
+ t.integer :post_id
25
+ t.integer :attr_id
26
+ t.string :value
27
+ end
28
+ end
29
+
30
+ class Post < ActiveRecord::Base
31
+ set_table_name :posts
32
+ end
33
+
34
+ class PostAttribute < ActiveRecord::Base
35
+ set_table_name :post_attributes
36
+ def eav_attr_name
37
+ name
38
+ end
39
+ end
40
+
41
+
42
+ class PostAttrValue < ActiveRecord::Base
43
+ set_table_name :post_attr_values
44
+ end
45
+
46
+ class EavPost < Post
47
+ include EavPresenter
48
+ eav :eav_attr_class => PostAttribute, :eav_value_class => PostAttrValue, :eav_entity_foreign_key => "post_id",
49
+ :eav_attr_foreign_key => "attr_id"
50
+ end
51
+
52
+ Post.create({:title => 'p1'})
53
+ Post.create({:title => 'p2'})
54
+ PostAttribute.create({:name => 'email'})
55
+ PostAttribute.create({:name => 'twitter'})
56
+ PostAttribute.create({:name => 'facebook'})
57
+ PostAttribute.create({:name => 'web'})
58
+
59
+
60
+ EavPost.create({:title => 'p3', :twitter => 'tw3', :web => ''})
61
+
62
+ =begin
63
+ ActiveRecord::Migration.class_eval do
64
+ drop_table :posts
65
+ drop_table :post_attribute
66
+ drop_table :post_attr_values
67
+ end
68
+ =end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: eav_presenter
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: "0.2"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nilesh Trivedi
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-30 00:00:00 +05:30
14
- default_executable:
13
+ date: 2012-03-19 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: activerecord
@@ -35,7 +34,7 @@ extra_rdoc_files: []
35
34
 
36
35
  files:
37
36
  - lib/eav_presenter.rb
38
- has_rdoc: true
37
+ - lib/test.rb
39
38
  homepage: http://github.com/nileshtrivedi/eav_presenter
40
39
  licenses: []
41
40
 
@@ -59,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
58
  requirements: []
60
59
 
61
60
  rubyforge_project:
62
- rubygems_version: 1.5.1
61
+ rubygems_version: 1.8.15
63
62
  signing_key:
64
63
  specification_version: 3
65
64
  summary: ActiveRecord model like presenter for Entity-Attribute-Value pattern