citier4 0.0.5 → 0.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c50f2f638ae88ccda915b992d306c497fb103d48
4
- data.tar.gz: cbcb12801ad32000c0ff2273a9ab5c22529aeb1e
3
+ metadata.gz: 09b3a5f7a3fa67c1f81f1c4f1712cf05dfab99df
4
+ data.tar.gz: f4eb2464924dd1a5996133372f5e7df8644685a8
5
5
  SHA512:
6
- metadata.gz: 09276f8264eefd438650e6b70879118277256fee99af534a8bd206ee54b8ae990b6be053e3182190351cb4ee019a44097913afb8f54effd9bc9b7dd2ca3036d1
7
- data.tar.gz: 30aa652b07ac0a2518cbfed45a41bac5e61c1ab36740b6918b50b79df7c5176c9aa7da6718dd483330d04b06db12433bcc7c0bedc78b4e12e05ca27fe173b0f0
6
+ metadata.gz: 3eac4f67749ab8a011bb6bb27d695962de49a5fa0600d8203f4093536e33f0a9dd4394f5943ba8ec1b3c797655eccf05079b621c68f58fd358a52bc42a007e5c
7
+ data.tar.gz: 9aae9a6a17d62d75e9f5f6b38e1b1520c9bb3616188ba4ace4d70b0e431eb543327ee16a9a5155a6f8fce0f3401fede20bb06e7e036ad733ed3ca4894d037c89
@@ -0,0 +1,2 @@
1
+ 0.0.6
2
+ * create_or_replace_citier_view (update ne marche pas quand la vue est référencé par une autre table)
@@ -38,7 +38,7 @@ module Citier4
38
38
  # Option for setting the inheritance columns, default value = 'type'
39
39
  # authorized only in root
40
40
  db_type_field = (options[:db_type_field] || :type).to_s
41
- puts "DODO : In Citier, we have to verify the presence of inheritance_column : #{db_type_field}"
41
+ citier_debug("DODO : In Citier, we have to verify the presence of inheritance_column : #{db_type_field}")
42
42
  self.inheritance_column = "#{db_type_field}"
43
43
 
44
44
  # Option for setting the super_class. Only in root
@@ -3,6 +3,30 @@ module Citier4
3
3
 
4
4
  citier_debug("Including Citier Child Instance")
5
5
 
6
+ def becomes_super
7
+ keys = self.class.superclass.attribute_names.dup # car sinon, c'est la même référence
8
+ keys.delete(self.class.inheritance_column)
9
+ parent = self.class.superclass.new self.attributes.slice(*keys)
10
+
11
+ # parent = self.class.superclass.new
12
+ parent.id = self.id # nul if create, a real id otherwise
13
+ parent.clear_changes_information
14
+ # Natural Rail STI process, in new, change the "type" colomn from nil to "class.superclass.name"
15
+ # so we have a flaged change for this "type" colomun.
16
+ # And we have to clear these change
17
+ # Clear all changes is more simple and probably more efficient that a specific clear on the STI "type" column
18
+ # like the line bellow
19
+ # parent.clear_attribute_change(parent.class.inheritance_column)
20
+ parent.force_attributes(attributes_for_parent)
21
+ # parent.force_attributes(attributes_for_parent, :merge => true)
22
+ # parent = self.class.superclass.new self.changed_attributes_for_parent
23
+ # these doen't work, because parent.class IS NOT self.superclass, but self.class
24
+ # sinon, TODO, pourquoi descendre en superclass. Il faut sauter et aller a la prochaine superclass dure ...
25
+ parent.is_new_record(self.new_record?)
26
+ parent.force_changed_attributes(changed_attributes_for_parent)
27
+ parent
28
+ end
29
+
6
30
  def save(options={})
7
31
  return false if (options[:validate] != false && !self.valid?)
8
32
 
@@ -31,30 +55,21 @@ module Citier4
31
55
  # Parent saving
32
56
 
33
57
  #create a new instance of the superclass, passing the inherited attributes.
34
-
35
- parent = self.class.superclass.new
36
- parent.id = self.id # nul if create, a real id otherwise
37
- parent.clear_changes_information
38
- # Natural Rail STI process, in new, change the "type" colomn from nil to "class.superclass.name"
39
- # so we have a flaged change for this "type" colomun.
40
- # And we have to clear these change
41
- # Clear all changes is more simple and probably more efficient that a specific clear on the STI "type" column
42
- # like the line bellow
43
- # parent.clear_attribute_change(parent.class.inheritance_column)
44
- parent.force_attributes(attributes_for_parent)
45
- # parent.force_attributes(attributes_for_parent, :merge => true)
46
- # parent = self.class.superclass.new self.changed_attributes_for_parent
47
- # these doen't work, because parent.class IS NOT self.superclass, but self.class
48
- # sinon, TODO, pourquoi descendre en superclass. Il faut sauter et aller a la prochaine superclass dure ...
49
- parent.is_new_record(self.new_record?)
50
-
51
- parent.force_changed_attributes(changed_attributes_for_parent)
52
- # parent.type = self.type
53
- # parent.is_new_record(new_record?)
54
-
58
+ # It is not good : new start callback, like after_initialise, and we doen't want to lauch such callback
59
+
60
+ #parent1 = self.class.superclass.new
61
+ #parent1.id = self.id # nul if create, a real id otherwise
62
+ #parent1.clear_changes_information
63
+ #parent1.force_attributes(attributes_for_parent)
64
+ #parent1.is_new_record(self.new_record?)
65
+ #parent1.force_changed_attributes(changed_attributes_for_parent)
66
+
67
+ parent = self.becomes_super
68
+ # parent = parent2
55
69
  # If we're root this will just be saved as normal through AR. If we're a child it will call this method again.
56
70
  # It will try and save it's parent and then save itself through the Writeable constant.
57
71
  # For a new record, we will have an id, only when the root is saved.
72
+
58
73
  parent_saved = parent.save
59
74
  # self.id = parent.id
60
75
 
@@ -71,12 +86,12 @@ module Citier4
71
86
  # il faut quand meme sauver le current. DOTO Faire un test.
72
87
  if !attributes_for_current.empty?
73
88
  # ici, parrent est sauvé, et on a un ID
74
- current = self.class::Writeable.new
75
- # current.force_attributes(attributes_for_current, :merge => true)
76
- current.force_attributes(attributes_for_current)
89
+ current = self.class::Writeable.new
90
+ # current.force_attributes(attributes_for_current, :merge => true)
91
+ current.force_attributes(attributes_for_current)
77
92
  # marche pas non plus current = self.class::Writeable.new attributes_for_current
78
93
 
79
- current.force_changed_attributes(changed_attributes_for_current)
94
+ current.force_changed_attributes(changed_attributes_for_current)
80
95
 
81
96
  # current.id = self.id # NON
82
97
  current.id = parent.id
@@ -24,6 +24,7 @@ class ActiveRecord::Base
24
24
  def self.create_class_root_writable(class_reference)
25
25
  Class.new(ActiveRecord::Base) do
26
26
  # include Citier4::InstanceMethods::ForcedWriters
27
+ include Citier4::RootInstanceMethods
27
28
 
28
29
  # set the name of the table associated to this class
29
30
  # this class will be associated to the writable table of the class_reference class
@@ -73,6 +74,7 @@ end
73
74
 
74
75
  class ActiveRecord::Migration
75
76
  #function for creating views for migrations
77
+
76
78
  def create_citier_view(theclass)
77
79
  # flush any column info in memory
78
80
  # Loops through and stops once we've cleaned up to our root class.
@@ -102,9 +104,42 @@ class ActiveRecord::Migration
102
104
  # citier_debug("Creating citier view -> #{sql}")
103
105
  #theclass.connection.execute sql
104
106
 
105
-
106
107
  end
107
108
 
109
+ def create_or_replace_citier_view(theclass)
110
+ # Convienience function for updating or creating views for migrations
111
+ # TODO : ugly code ... (c'est le meme que create_citier, avec juste l'appel à create_or_update )
112
+ #
113
+ # flush any column info in memory
114
+ # Loops through and stops once we've cleaned up to our root class.
115
+ # We MUST user Writeable as that is the place where changes might reside!
116
+ reset_class = theclass::Writeable
117
+ until reset_class == ActiveRecord::Base # Refinery::Core::BaseModel # ActiveRecord::Base eoz
118
+ citier_debug("Resetting column information on #{reset_class}")
119
+ reset_class.reset_column_information
120
+ reset_class = reset_class.superclass
121
+ end
122
+
123
+ self_columns = theclass::Writeable.column_names.select{ |c| c != "id" }
124
+ parent_columns = theclass.superclass.column_names.select{ |c| c != "id" }
125
+ columns = parent_columns+self_columns
126
+ self_read_table = theclass.table_name
127
+ self_write_table = theclass::Writeable.table_name
128
+ parent_read_table = theclass.superclass.table_name
129
+ # sql = "CREATE VIEW #{self_read_table} AS SELECT #{parent_read_table}.id, #{columns.join(',')} FROM #{parent_read_table}, #{self_write_table} WHERE #{parent_read_table}.id = #{self_write_table}.id"
130
+ #Use our rails_sql_views gem to create the view so we get it outputted to schema
131
+ create_or_replace_view "#{self_read_table}", "SELECT #{parent_read_table}.id, #{columns.join(',')} FROM #{parent_read_table}, #{self_write_table} WHERE #{parent_read_table}.id = #{self_write_table}.id" do |v|
132
+ v.column :id
133
+ columns.each do |c|
134
+ v.column c.to_sym
135
+ end
136
+ end
137
+
138
+ # citier_debug("Creating citier view -> #{sql}")
139
+ #theclass.connection.execute sql
140
+
141
+ end
142
+
108
143
  def drop_citier_view(theclass) #function for dropping views for migrations
109
144
  self_read_table = theclass.table_name
110
145
  # sql = "DROP VIEW #{self_read_table}"
@@ -119,22 +154,13 @@ class ActiveRecord::Migration
119
154
  citier_debug("Updating citier view for #{theclass}")
120
155
  if theclass.table_exists?
121
156
  drop_citier_view(theclass)
157
+ # si la view est déjà utilisé, impossible de la délété.
122
158
  create_citier_view(theclass)
123
159
  else
124
160
  citier_debug("Error: #{theclass} VIEW doesn't exist.")
125
161
  end
126
162
  end
127
163
 
128
- def create_or_update_citier_view(theclass) #Convienience function for updating or creating views for migrations
129
- citier_debug("Create or Update citier view for #{theclass}")
130
- if theclass.table_exists?
131
- update_citier_view(theclass)
132
- else
133
- citier_debug("VIEW DIDN'T EXIST. Now creating for #{theclass}")
134
- create_citier_view(theclass)
135
- end
136
- end
137
-
138
164
  end
139
165
 
140
166
  module ActiveRecord
@@ -27,13 +27,15 @@ module Citier4
27
27
  end
28
28
 
29
29
  module ForcedWriters
30
+
30
31
  def force_attributes(new_attributes, options = {})
31
- # debugger
32
32
  # TODO new_attributes = @attributes.merge(new_attributes) if options[:merge]
33
33
  new_attributes = self.attributes.merge(new_attributes) if options[:merge]
34
34
 
35
35
  # TODO @attributes = new_attributes
36
36
  self.attributes = new_attributes
37
+ # remplace la valeur d'un attribut par celle fournie,
38
+ # Ne change pas la liste des attributs.
37
39
 
38
40
  if options[:clear_caches] != false
39
41
  @aggregation_cache = {}
@@ -48,6 +50,37 @@ module Citier4
48
50
  new_changed_attributes = @attributes.merge(new_changed_attributes) if options[:merge]
49
51
  @changed_attributes = new_changed_attributes
50
52
  end
53
+
54
+
55
+ def becomes_super_old
56
+ # debugger
57
+ keys = self.class.superclass.attribute_names.dup # car sinon, c'est la même référence
58
+ keys.delete(self.class.inheritance_column)
59
+ became = self.class.superclass.new self.attributes.slice(*keys)
60
+ became.id = self.id
61
+ became[self.class.inheritance_column] = self[self.class.inheritance_column]
62
+ became.clear_changes_information
63
+ #became.instance_variable_set("@changed_attributes", @changed_attributes.slice(*keys)) if defined?(@changed_attributes)
64
+ became.force_attributes(attributes_for_parent)
65
+ became.force_changed_attributes(changed_attributes_for_parent)
66
+ became.instance_variable_set("@new_record", new_record?)
67
+ became.instance_variable_set("@destroyed", destroyed?)
68
+ became.instance_variable_set("@errors", errors)
69
+ became
70
+ end
71
+
72
+ def becomes_super_old1
73
+ #keys = self.class.superclass.attribute_names.dup # car sinon, c'est la même référence
74
+ #keys.delete(self.class.inheritance_column)
75
+ became = self.class.superclass.new # self.attributes.slice(*keys)
76
+ became.id = self.id # nul if create, a real id otherwise
77
+ became.clear_changes_information
78
+ became.force_attributes(attributes_for_parent)
79
+ became.is_new_record(self.new_record?)
80
+ became.force_changed_attributes(changed_attributes_for_parent)
81
+ became
82
+ end
83
+
51
84
  end
52
85
 
53
86
  # USAGE validates :attribute, :citier_uniqueness => true
@@ -29,8 +29,6 @@ module Citier4
29
29
  where(*args).take!
30
30
  rescue RangeError
31
31
  raise RecordNotFound, "Couldn't find #{@klass.name} with an out of range value"
32
- end
33
-
34
-
35
- end
32
+ end
33
+ end
36
34
  end
@@ -43,6 +43,9 @@ module Citier4
43
43
  # self.class.superclass==ActiveRecord::Base # eoz
44
44
  # self.class.superclass==Refinery::Core::BaseModel
45
45
  end
46
-
46
+
47
+ def root_initialize
48
+ @attributes = self.class._default_attributes.dup
49
+ end
47
50
  end
48
51
  end
@@ -1,3 +1,3 @@
1
1
  module Citier4
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -11,6 +11,7 @@ end
11
11
 
12
12
 
13
13
  test "changed in find in root or child" do
14
+ debugger
14
15
  book = Book.first
15
16
  book_changed = book.changed
16
17
 
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  class ChildInstanceMethods < ActiveSupport::TestCase
4
4
 
5
5
  test "book instance is not root" do
6
- assert_not Book.new.is_root?
6
+ assert_not Book.new(name: 'my book').is_root?
7
7
  Book.create( name: "UnLivre", author: "Inconnu")
8
8
  book = Book.find_by( name: "UnLivre")
9
9
  assert book
@@ -0,0 +1,14 @@
1
+ class Foo
2
+
3
+ def initialize( attributes = nil, options = {})
4
+ debugger
5
+ # super(attributes, options)
6
+ puts "ici, in initalize of #{self.class.name}: #{attributes}"
7
+ end
8
+ end
9
+
10
+ class Bar < Foo
11
+
12
+ # Bar.new : Appel de une ou deux fois initialize ?
13
+
14
+ end
@@ -1,2 +1,10 @@
1
1
  class Bidule < ActiveRecord::Base
2
+
3
+ def initialize( attributes = nil, options = {})
4
+ debugger
5
+ super(attributes, options)
6
+ puts "ici, in initalize #{attributes}"
7
+ end
8
+
9
+
2
10
  end
@@ -1,7 +1,162 @@
1
+
2
+ #:after_initialize
3
+ #:after_find
4
+ #:after_touch
5
+ #:before_validation
6
+ #:after_validation
7
+ #:before_save
8
+ #:around_save
9
+ #:after_save
10
+ #:before_create
11
+ #:around_create
12
+ #:after_create
13
+ #:before_update
14
+ #:around_update
15
+ #:after_update
16
+ #:before_destroy
17
+ #:around_destroy
18
+ #:after_destroy
19
+ #:after_commit
20
+ #:after_rollback
21
+
22
+ # http://edgeguides.rubyonrails.org/active_record_callbacks.html
23
+
24
+
1
25
  class Product < ActiveRecord::Base
2
26
 
3
27
  acts_as_citier4
4
28
 
5
29
  validates :name, presence: true
6
30
 
31
+
32
+
33
+ def initialize( attributes = nil, options = {})
34
+ # debugger
35
+ super(attributes, options)
36
+ if !name
37
+ raise "name can not be nil or blanck in initialize"
38
+ end
39
+ puts "ici, in initalize of #{self.class.name}: #{attributes}"
40
+ puts "my name is : #{name}" # Just to trigger an error if name is not present
41
+ end
42
+
43
+ def hello
44
+ puts "coucou"
45
+ end
46
+
47
+ def clone_root
48
+ debugger
49
+ @attributes = self.class._default_attributes.dup
50
+ end
51
+
52
+ def toto_initialize_copy(source)
53
+ # super
54
+ source.instance_variables.each do |var|
55
+ src_value = source.instance_variable_get(var)
56
+ value = src_value.clone rescue src_value
57
+ instance_variable_set(var, value)
58
+ end
59
+ end
60
+
61
+
62
+ def rails_dup
63
+ debugger
64
+ dup
65
+ end
66
+
67
+ def product_becomes( kclass)
68
+ debugger
69
+ becomes(kclass)
70
+ end
71
+
72
+ def rails_becomes( klass)
73
+ debugger
74
+ became = klass.new
75
+ # skip_callback :after_save, :save_cookie
76
+
77
+ # became.instance_variable_set("@attributes", @attributes)
78
+ # pas bon, recopie tout
79
+ # marche presque
80
+ # became.attributes= self.attributes
81
+ #
82
+ became.attributes = self.attributes.slice(*became.attributes.keys)
83
+ # became.instance_variable_set("@changed_attributes", @changed_attributes) if defined?(@changed_attributes)
84
+ # c'est ok, mais farie l'intersect.
85
+ became.instance_variable_set("@changed_attributes", @changed_attributes.slice(*became.changed_attributes.keys)) if defined?(@changed_attributes)
86
+ became.instance_variable_set("@new_record", new_record?)
87
+ became.instance_variable_set("@destroyed", destroyed?)
88
+ became.instance_variable_set("@errors", errors)
89
+ became
90
+ end
91
+
92
+ def dup_becomes( klass)
93
+ debugger
94
+ # became = klass.new
95
+ # became = self.dup
96
+ became = klass.allocate
97
+ became.instance_variable_set("@attributes", became.class._default_attributes.dup)
98
+
99
+
100
+ became.attributes = self.attributes.slice(*became.class.attribute_names)
101
+ # became.instance_variable_set("@changed_attributes", @changed_attributes) if defined?(@changed_attributes)
102
+ # c'est ok, mais farie l'intersect.
103
+ became.instance_variable_set("@changed_attributes", @changed_attributes.slice(*became.changed_attributes.keys)) if defined?(@changed_attributes)
104
+ became.instance_variable_set("@new_record", new_record?)
105
+ became.instance_variable_set("@destroyed", destroyed?)
106
+ became.instance_variable_set("@errors", errors)
107
+ became
108
+
109
+ end
110
+
111
+ def becomes_klass(klass)
112
+ debugger
113
+ save_attributes = self.attributes
114
+ @attributes = klass._default_attributes.dup
115
+ self.attributes = save_attributes.slice(*klass.attribute_names)
116
+ @changed_attributes = @changed_attributes.slice(*klass.attribute_names) if defined?(@changed_attributes)
117
+ end
118
+
119
+ #parent1 = self.class.superclass.new self.attributes.slice(*self.class.superclass.attribute_names) # il faut enlever le type pour éviter le STI
120
+ #parent2 = self.class.superclass.allocate
121
+ #parent2.root_initialize
122
+ #parent2.toto_initialize_copy(self)
123
+ #parent_writeable_class = self.class.superclass.const_get :Writeable
124
+ #parent3 = parent_writeable_class.new
125
+
126
+ # get instance variable : instance_variable_get(("@" + name).intern)
127
+
128
+ def product_dup
129
+ debugger
130
+ dup = self.class.allocate
131
+ dup.copy_instance_variables(self)
132
+ dup.initialize_dup(self)
133
+ dup
134
+ end
135
+
136
+ def private_attributes
137
+ @attributes
138
+ end
139
+
140
+ def initialize_copy(source)
141
+ debugger
142
+ puts "Copy: #{source.inspect}"
143
+ super
144
+ end
145
+
146
+ def initialize_dup(source)
147
+ debugger
148
+ puts "Dup: #{source.inspect}"
149
+ super
150
+ end
151
+
152
+ def initialize_clone(source)
153
+ debugger
154
+ puts "Clone: #{source.inspect}"
155
+ super
156
+ end
157
+
158
+
159
+ #http://aaronlasseigne.com/2014/07/20/know-ruby-clone-and-dup/
160
+
161
+
7
162
  end