embedded_associations 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -9,4 +9,6 @@ group :test do
9
9
 
10
10
  gem 'rspec'
11
11
  gem 'rspec-rails'
12
+
13
+ gem 'pry'
12
14
  end
@@ -1,3 +1,3 @@
1
1
  module EmbeddedAssociations
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -31,9 +31,7 @@ module EmbeddedAssociations
31
31
  end
32
32
 
33
33
  # Simple callbacks for now, eventually should use a filter system
34
- def before_embedded_update(record); end
35
- def before_embedded_create(record); end
36
- def before_embedded_destroy(record); end
34
+ def before_embedded(record, action); end
37
35
 
38
36
  class Definitions
39
37
  include Enumerable
@@ -102,6 +100,10 @@ module EmbeddedAssociations
102
100
  end
103
101
  end
104
102
 
103
+ def filter_attributes(name, attrs, action)
104
+ attrs
105
+ end
106
+
105
107
  def handle_plural_resource(parent, name, attr_array, child_definition)
106
108
  current_assoc = parent.send(name)
107
109
 
@@ -116,11 +118,13 @@ module EmbeddedAssociations
116
118
  if id = attrs['id']
117
119
  # can't use current_assoc.find(id), see http://stackoverflow.com/questions/11605120/autosave-ignored-on-has-many-relation-what-am-i-missing
118
120
  r = current_assoc.find{|r| r.id == id.to_i}
121
+ attrs = filter_attributes(r.class.name, attrs, :update)
119
122
  handle_resource(child_definition, r, attrs) if child_definition
120
123
  r.assign_attributes(attrs)
121
124
  run_before_update_callbacks(r)
122
125
  else
123
126
  r = current_assoc.build()
127
+ attrs = filter_attributes(r.class.name, attrs, :create)
124
128
  handle_resource(child_definition, r, attrs) if child_definition
125
129
  r.assign_attributes(attrs)
126
130
  run_before_create_callbacks(r)
@@ -130,8 +134,10 @@ module EmbeddedAssociations
130
134
 
131
135
  def handle_singular_resource(parent, name, attrs, child_definition)
132
136
  current_assoc = parent.send(name)
137
+
133
138
  if r = current_assoc
134
139
  if attrs
140
+ attrs = filter_attributes(r.class.name, attrs, :update)
135
141
  handle_resource(child_definition, r, attrs) if child_definition
136
142
  r.assign_attributes(attrs)
137
143
  run_before_update_callbacks(r)
@@ -142,6 +148,7 @@ module EmbeddedAssociations
142
148
  end
143
149
  elsif attrs
144
150
  r = parent.send("build_#{name}")
151
+ attrs = filter_attributes(r.class.name, attrs, :create)
145
152
  handle_resource(child_definition, r, attrs) if child_definition
146
153
  r.assign_attributes(attrs)
147
154
  run_before_create_callbacks(r)
@@ -149,15 +156,15 @@ module EmbeddedAssociations
149
156
  end
150
157
 
151
158
  def run_before_create_callbacks(record)
152
- controller.send(:before_embedded_create, record)
159
+ controller.send(:before_embedded, record, :create)
153
160
  end
154
161
 
155
162
  def run_before_update_callbacks(record)
156
- controller.send(:before_embedded_update, record)
163
+ controller.send(:before_embedded, record, :update)
157
164
  end
158
165
 
159
166
  def run_before_destroy_callbacks(record)
160
- controller.send(:before_embedded_destroy, record)
167
+ controller.send(:before_embedded, record, :destroy)
161
168
  end
162
169
  end
163
170
 
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'bundler'
2
2
  Bundler.require(:default, :test)
3
3
 
4
+ ENV['RAILS_ENV'] = 'test'
5
+
4
6
  PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
5
7
  $LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
6
8
 
@@ -11,6 +13,10 @@ require 'rspec/rails'
11
13
 
12
14
  require 'support/serialization_helpers'
13
15
 
16
+ puts File.expand_path("../support/app/db/migrate/", __FILE__)
17
+
18
+ ActiveRecord::Migrator.migrate(File.expand_path("../support/app/db/migrate/", __FILE__))
19
+
14
20
  RSpec.configure do |config|
15
21
  config.use_transactional_fixtures = true
16
22
  end
@@ -9,6 +9,7 @@
9
9
 
10
10
  # Ignore the default SQLite database.
11
11
  /db/*.sqlite3
12
+ /db/*.sqlite3-journal
12
13
 
13
14
  # Ignore all logfiles and tempfiles.
14
15
  /log/*.log
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embedded_associations
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:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-27 00:00:00.000000000 Z
12
+ date: 2013-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -86,7 +86,6 @@ files:
86
86
  - spec/support/app/db/migrate/20130226001639_create_categories.rb
87
87
  - spec/support/app/db/schema.rb
88
88
  - spec/support/app/db/seeds.rb
89
- - spec/support/app/db/structure.sql
90
89
  - spec/support/app/lib/assets/.gitkeep
91
90
  - spec/support/app/lib/tasks/.gitkeep
92
91
  - spec/support/app/log/.gitkeep
@@ -159,7 +158,6 @@ test_files:
159
158
  - spec/support/app/db/migrate/20130226001639_create_categories.rb
160
159
  - spec/support/app/db/schema.rb
161
160
  - spec/support/app/db/seeds.rb
162
- - spec/support/app/db/structure.sql
163
161
  - spec/support/app/lib/assets/.gitkeep
164
162
  - spec/support/app/lib/tasks/.gitkeep
165
163
  - spec/support/app/log/.gitkeep
@@ -1,19 +0,0 @@
1
- CREATE TABLE "accounts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "note" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
2
- CREATE TABLE "categories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
3
- CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer, "user_id" integer, "content" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
4
- CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "user_id" integer, "category_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
5
- CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
6
- CREATE TABLE "tags" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer, "name" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
7
- CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
8
- CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
9
- INSERT INTO schema_migrations (version) VALUES ('20130225045501');
10
-
11
- INSERT INTO schema_migrations (version) VALUES ('20130225045512');
12
-
13
- INSERT INTO schema_migrations (version) VALUES ('20130225173707');
14
-
15
- INSERT INTO schema_migrations (version) VALUES ('20130225173717');
16
-
17
- INSERT INTO schema_migrations (version) VALUES ('20130226001629');
18
-
19
- INSERT INTO schema_migrations (version) VALUES ('20130226001639');