extend_at 0.2.1 → 0.2.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.
- data/.gitignore +2 -1
- data/CHANGELOG.txt +5 -0
- data/lib/extend_at/configuration.rb +32 -4
- data/lib/extend_at/model_manager.rb +1 -1
- data/lib/extend_at/version.rb +1 -1
- data/lib/extend_at.rb +54 -22
- data/spec/extend_at_spec.rb +27 -0
- metadata +9 -9
data/.gitignore
CHANGED
data/CHANGELOG.txt
CHANGED
@@ -1,3 +1,8 @@
|
|
1
1
|
0.2.1:
|
2
2
|
* Fixed some problems.
|
3
3
|
* Internal functionality changed (preparing for association support like belongs_to)
|
4
|
+
|
5
|
+
0.2.2:
|
6
|
+
* Some clean up code.
|
7
|
+
* Fixed problem: Validation not work when the extention is static
|
8
|
+
* Fixed proble: Models fail when save and the extention is not used (mass assignment).
|
@@ -2,8 +2,12 @@ require File.expand_path('../environment', __FILE__)
|
|
2
2
|
|
3
3
|
module ExtendModelAt
|
4
4
|
class Configuration
|
5
|
-
def
|
6
|
-
|
5
|
+
def run(env=nil,model=nil)
|
6
|
+
if env.kind_of? Hash
|
7
|
+
hash = expand_options env, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] }, model.clone
|
8
|
+
hash[:columns] = init_columns hash[:columns]
|
9
|
+
return hash
|
10
|
+
end
|
7
11
|
|
8
12
|
if not env.kind_of? Proc
|
9
13
|
return {}
|
@@ -13,7 +17,31 @@ module ExtendModelAt
|
|
13
17
|
end
|
14
18
|
|
15
19
|
protected
|
16
|
-
def
|
20
|
+
def init_columns(columns={})
|
21
|
+
new = {}
|
22
|
+
columns.each do |column, config|
|
23
|
+
new[column] = config
|
24
|
+
# Stablish the type
|
25
|
+
if config[:type].class == Class
|
26
|
+
# If exist :type, is a static column
|
27
|
+
new[column][:type] = get_type_for_class config[:type]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
new
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_type_for_class(type)
|
34
|
+
type = type.name
|
35
|
+
return :any if type == 'NilClass'
|
36
|
+
return :float if type == 'Float'
|
37
|
+
return :integer if type == 'Fixnum'
|
38
|
+
return :text if type == 'String '
|
39
|
+
return :timestamp if type == 'Time'
|
40
|
+
return :datetime if type == 'Date'
|
41
|
+
return :any
|
42
|
+
end
|
43
|
+
|
44
|
+
def expand_options(options={}, opts={}, model=nil)
|
17
45
|
options = get_value_of options, model
|
18
46
|
config_opts = {
|
19
47
|
:not_expand => [],
|
@@ -38,7 +66,7 @@ module ExtendModelAt
|
|
38
66
|
end
|
39
67
|
end
|
40
68
|
|
41
|
-
def
|
69
|
+
def get_value_of(value, model=nil)
|
42
70
|
if value.kind_of? Symbol
|
43
71
|
# If the function exist, we execute it
|
44
72
|
if model.respond_to? value
|
@@ -5,7 +5,7 @@ module ExtendModelAt
|
|
5
5
|
class ModelManager
|
6
6
|
def initialize(column_name,model, config)
|
7
7
|
@column_name, @model, @config = column_name, model, config
|
8
|
-
|
8
|
+
|
9
9
|
@extend_at = ExtendAt.find_by_model_id_and_model_type @model.id, @model.class.to_s
|
10
10
|
if @extend_at.nil?
|
11
11
|
@extend_at = ExtendAt.new
|
data/lib/extend_at/version.rb
CHANGED
data/lib/extend_at.rb
CHANGED
@@ -19,11 +19,11 @@ module ExtendModelAt
|
|
19
19
|
# The object how controll the data
|
20
20
|
class Extention
|
21
21
|
def initialize(options={})
|
22
|
-
@configuration = ExtendModelAt::Configuration.run options, options[:model]
|
23
|
-
@model_manager = ::ExtendModelAt::ModelManager.new(@column_name,
|
22
|
+
@configuration = ExtendModelAt::Configuration.new.run options, options[:model].clone
|
23
|
+
@model_manager = ::ExtendModelAt::ModelManager.new(@configuration[:column_name].to_s, options[:model], @configuration)
|
24
24
|
|
25
25
|
@static = @configuration[:static] || false
|
26
|
-
@model = @configuration[:model]
|
26
|
+
@model = @configuration[:model].clone
|
27
27
|
@column_name = @configuration[:column_name].to_s
|
28
28
|
@columns = @configuration[:columns]
|
29
29
|
@value = get_defaults_values @configuration
|
@@ -87,8 +87,37 @@ module ExtendModelAt
|
|
87
87
|
@configuration
|
88
88
|
end
|
89
89
|
|
90
|
-
|
91
|
-
|
90
|
+
def configuration=(value)
|
91
|
+
@configuration = value
|
92
|
+
end
|
93
|
+
|
94
|
+
def define_associationss
|
95
|
+
[:has_one, :has_many, :belongs_to].each do |relation|
|
96
|
+
eval <<-EOS
|
97
|
+
if @configuration.keys.include? :#{relation}
|
98
|
+
raise "Invalid #{relation} value" if not [Hash, Array, NilClass].include? @configuration[:#{relation}].class
|
99
|
+
# We nee an array of models, then, we
|
100
|
+
if @configuration[:#{relation}].kind_of? Hash
|
101
|
+
list_models = @configuration[:#{relation}].keys
|
102
|
+
elsif @configuration[:#{relation}].kind_of? Array
|
103
|
+
list_models = @configuration[:#{relation}]
|
104
|
+
else
|
105
|
+
list_models = [@configuration[:#{relation}]]
|
106
|
+
end
|
107
|
+
list_models.each do |model|
|
108
|
+
define_method(model.to_sym) do |force_reload|
|
109
|
+
if @configuration[:#{relation}].kind_of? Hash
|
110
|
+
config = @configuration[:#{relation}][model]
|
111
|
+
else
|
112
|
+
config = {}
|
113
|
+
end
|
114
|
+
@model_manager.read_#{relation} model, @configuration[:#{relation}][model], force_reload
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
EOS
|
119
|
+
end
|
120
|
+
end
|
92
121
|
|
93
122
|
def get_adapter(column, value)
|
94
123
|
if @columns[column.to_sym][:type] == String
|
@@ -239,10 +268,9 @@ module ExtendModelAt
|
|
239
268
|
|
240
269
|
define_method(column_name.to_s) do
|
241
270
|
if not @extend_at_configuration.kind_of? ExtendModelAt::Extention
|
242
|
-
options[:model] = self
|
243
|
-
@extend_at_configuration
|
244
|
-
|
245
|
-
initialize_columns @extend_at_configuration.send(:configuration)[:columns] if options.kind_of? Hash
|
271
|
+
options[:model] = self.clone
|
272
|
+
@extend_at_configuration = ExtendModelAt::Extention.new(options )
|
273
|
+
initialize_columns @extend_at_configuration.send(:configuration)[:columns] || {}
|
246
274
|
end
|
247
275
|
@extend_at_configuration
|
248
276
|
end
|
@@ -261,11 +289,21 @@ module ExtendModelAt
|
|
261
289
|
end
|
262
290
|
end
|
263
291
|
|
292
|
+
def set_extend_at_validation(value={})
|
293
|
+
@extend_at_validation
|
294
|
+
end
|
295
|
+
|
296
|
+
def update_model_manager
|
297
|
+
@extend_at_configuration.send :update_model_manager if @extend_at_configuration.respond_to? :update_model_manager
|
298
|
+
end
|
299
|
+
|
264
300
|
# Initialize each column configuration
|
265
301
|
def initialize_columns(columns = {})
|
302
|
+
colunms_config = {}
|
266
303
|
columns.each do |column, config|
|
267
|
-
initialize_column column, config
|
304
|
+
colunms_config[column.to_sym] = initialize_column column, config
|
268
305
|
end
|
306
|
+
colunms_config
|
269
307
|
end
|
270
308
|
|
271
309
|
def initialize_column(column,config={})
|
@@ -305,7 +343,6 @@ module ExtendModelAt
|
|
305
343
|
raise ExtendModelAt::ArgumentError, "The validation of \#\{column\} is invalid"
|
306
344
|
end
|
307
345
|
|
308
|
-
|
309
346
|
column_config
|
310
347
|
end
|
311
348
|
|
@@ -322,15 +359,10 @@ module ExtendModelAt
|
|
322
359
|
end
|
323
360
|
|
324
361
|
def create_validation_for(column, validation)
|
325
|
-
column = column.to_sym
|
326
362
|
@extend_at_validation ||= {}
|
327
363
|
@extend_at_validation[column] = validation
|
328
364
|
end
|
329
365
|
|
330
|
-
def update_model_manager
|
331
|
-
@extend_at_configuration.send :update_model_manager if @extend_at_configuration.respond_to? :update_model_manager
|
332
|
-
end
|
333
|
-
|
334
366
|
def get_type_for_class(type)
|
335
367
|
type = type.name
|
336
368
|
return :any if type == 'NilClass'
|
@@ -355,12 +387,12 @@ module ExtendModelAt
|
|
355
387
|
end
|
356
388
|
|
357
389
|
def valid_type?(value, type)
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
390
|
+
type = type.to_s.to_sym
|
391
|
+
[:"", :any].include? type or
|
392
|
+
value.nil? or
|
393
|
+
(type == :boolean and ([true.class, false.class].include? value.class)) or
|
394
|
+
((not [:boolean, nil].include?(type)) and not value.nil? and compatible_type value, type )
|
395
|
+
end
|
364
396
|
end
|
365
397
|
end
|
366
398
|
end
|
data/spec/extend_at_spec.rb
CHANGED
@@ -9,6 +9,33 @@ describe 'extend_at' do
|
|
9
9
|
article.extra.respond_to?(:last_name).should == true
|
10
10
|
end
|
11
11
|
|
12
|
+
it 'without mass assignment should work' do
|
13
|
+
article = Article.new
|
14
|
+
article.save
|
15
|
+
article.extra.last_name = "Gonzales"
|
16
|
+
article.save
|
17
|
+
article.reload
|
18
|
+
article.extra.last_name.should == "Gonzales"
|
19
|
+
|
20
|
+
user = User.new
|
21
|
+
user.save
|
22
|
+
user.private_info.real_name = "Pedro"
|
23
|
+
user.save
|
24
|
+
user.reload
|
25
|
+
user.private_info.real_name.should == "Pedro"
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'when is loaded, should load the extra information' do
|
29
|
+
article = Article.new :extra_name => 'Pedro', :extra_last_name => 'Gonzales'
|
30
|
+
article.extra.name.should == 'Pedro'
|
31
|
+
article.extra.last_name.should == 'Gonzales'
|
32
|
+
article.save
|
33
|
+
|
34
|
+
article = Article.last
|
35
|
+
article.extra.name.should == "Pedro"
|
36
|
+
article.extra.last_name.should == "Gonzales"
|
37
|
+
end
|
38
|
+
|
12
39
|
it "mass assignment" do
|
13
40
|
article = Article.new :extra_name => 'Pedro', :extra_last_name => 'Gonzales'
|
14
41
|
article.extra.name.should == 'Pedro'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: extend_at
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-01-25 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &6926540 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *6926540
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &6925900 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.5'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *6925900
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec-core
|
38
|
-
requirement: &
|
38
|
+
requirement: &6925520 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *6925520
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: database_cleaner
|
49
|
-
requirement: &
|
49
|
+
requirement: &5741880 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0.7'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *5741880
|
58
58
|
description: ! 'This gem allows you to extend rails models without migrations: This
|
59
59
|
way you can, i.e., develop your own content types, like in Drupal.'
|
60
60
|
email:
|