extend_at 0.1.5 → 0.2.0

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 CHANGED
@@ -1,2 +1,3 @@
1
1
  .directory
2
- *~
2
+ *~
3
+ *Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ script: bundle exec rake
data/Gemfile CHANGED
@@ -6,4 +6,5 @@ gemspec
6
6
 
7
7
  group :test do
8
8
  gem "rails", "~> 3.1.0"
9
- end
9
+ gem "sqlite3"
10
+ end
data/Gemfile.ci ADDED
@@ -0,0 +1,10 @@
1
+ case ENV['DB']
2
+ when "sqlite"; gem "sqlite3"
3
+ end
4
+
5
+ def gem(*args)
6
+ super unless %w(pg sqlite3).include?(args.first)
7
+ end
8
+
9
+ # Eval Gemfile
10
+ eval(IO.read(File.join(File.dirname(__FILE__), 'Gemfile')), binding)
data/README.markdown CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  This gem allows you to extend models without migrations: This way you can, i.e., develop your own content types, like in Drupal.
4
4
 
5
- ## NOTE:
5
+ ## Status:
6
6
 
7
- Incompatible with previous versions.
7
+ [![Build Status](https://secure.travis-ci.org/anga/extend_at.png)](http://travis-ci.org/anga/extend_at)
8
8
 
9
9
  ## Installation
10
10
 
@@ -116,8 +116,6 @@ But you can use classes.
116
116
 
117
117
  Else, return <code>:any</code>
118
118
 
119
- You can use any class, but if you need use boolean values, you must use :boolean.
120
-
121
119
  #### Set default value
122
120
 
123
121
  class User < ActiveRecord::Base
@@ -159,6 +157,7 @@ You can use any class, but if you need use boolean values, you must use :boolean
159
157
  end
160
158
 
161
159
  #### Set validation
160
+
162
161
  class User < ActiveRecord::Base
163
162
  extend_at :extra, :columns => {
164
163
  :private_photos => {
@@ -215,6 +214,21 @@ You can use any class, but if you need use boolean values, you must use :boolean
215
214
  end
216
215
  end
217
216
 
217
+ ### Static columns
218
+
219
+ If you like to restrict the existent extended columns, you should use <code>:static => true</code>
220
+
221
+ class User < ActiveRecord::Base
222
+ extend_at :extra, :columns => {
223
+ :private_photos => {
224
+ :type => :boolean,
225
+ :default => true
226
+ }
227
+ }, :static => true
228
+ end
229
+
230
+ Now, <code>User.extra</code> only accept <code>private_photos</code> column
231
+
218
232
  ### Scopes
219
233
 
220
234
  You can use scope like:
@@ -286,11 +300,6 @@ This code read the configuration of the columns when you access to the extra col
286
300
 
287
301
  If you found a bug, create an issue. If you have a recomendation, idea, etc., create a request or fork the project.
288
302
 
289
- ## TODO:
290
-
291
- * RSpec test
292
- * Support static columns
293
-
294
303
  ## License
295
304
 
296
- This gem is under MIT license.
305
+ This gem is under MIT license.
data/Rakefile CHANGED
@@ -8,5 +8,5 @@ task :default => :spec
8
8
  task :test => :spec
9
9
 
10
10
  RSpec::Core::RakeTask.new(:spec) do
11
- system "echo \"recreating database \" && cd #{File.join(File.dirname(__FILE__), 'spec', 'app')} && rake db:migrate:reset"
12
- end
11
+ system "echo \"recreating database \" && cd #{File.join(File.dirname(__FILE__), 'spec', 'app')} && bundle install && rake db:migrate:reset"
12
+ end
@@ -1,3 +1,3 @@
1
1
  module ExtendModelAt
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/extend_at.rb CHANGED
@@ -8,16 +8,24 @@ module ExtendModelAt
8
8
  base.extend(ClassMethods)
9
9
  end
10
10
 
11
+ # Errors
12
+ class InvalidColumn < Exception
13
+ end
14
+
15
+ class ArgumentError < Exception
16
+ end
17
+
11
18
  # The object how controll the data
12
19
  class Extention
13
20
  def initialize(options={})
21
+ @static = options[:static] || false
14
22
  @model = options[:model]
15
23
  @column_name = options[:column_name].to_s
16
24
  @columns = options[:columns]
17
25
  @value = get_defaults_values options
18
26
  @model_manager = ::ExtendModelAt::ModelManager.new(@column_name, @model, @columns)
19
27
 
20
- raise "#{@column_name} should by text or string not #{options[:model].column_for_attribute(@column_name.to_sym).type}" if not [:text, :stiring].include? options[:model].column_for_attribute(@column_name.to_sym).type
28
+ raise ExtendModelAt::ArgumentError, "#{@column_name} should by text or string not #{options[:model].column_for_attribute(@column_name.to_sym).type}" if not [:text, :stiring].include? options[:model].column_for_attribute(@column_name.to_sym).type
21
29
 
22
30
  initialize_values
23
31
  end
@@ -30,7 +38,7 @@ module ExtendModelAt
30
38
  if not valid_type? value, @columns[key.to_sym].try(:[],:type)
31
39
  # Try to adapt the value
32
40
  adapter = get_adapter key, value
33
- raise "#{value.inspect} is not a valid type, expected #{@columns[key.to_sym][:type]}" if adapter.nil? # We can't adapt the value
41
+ raise ExtendModelAt::ArgumentError, "#{value.inspect} is not a valid type, expected #{@columns[key.to_sym][:type]}" if adapter.nil? # We can't adapt the value
34
42
  value = value.send adapter
35
43
  end
36
44
  @value[key.to_s] = value
@@ -59,12 +67,13 @@ module ExtendModelAt
59
67
 
60
68
  # Use the undefined method as a column
61
69
  def method_missing(m, *args, &block)
70
+ column_name = m.to_s.gsub(/\=$/, '')
71
+ raise ExtendModelAt::InvalidColumn, "#{column_name} not exist" if @static == true and not (@columns.try(:keys).try(:include?, column_name.to_sym) )
62
72
  # If the method don't finish with "=" is fore read
63
73
  if m !~ /\=$/
64
74
  self[m.to_s]
65
75
  # but if finish with "=" is for wirte
66
76
  else
67
- column_name = m.to_s.gsub(/\=$/, '')
68
77
  self[column_name.to_s] = args.first
69
78
  end
70
79
  end
@@ -225,7 +234,7 @@ module ExtendModelAt
225
234
  :extensible => true # If is false, only the columns defined in :columns can be used
226
235
  }.merge!(opts)
227
236
  columns = initialize_columns expand_options(options, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] }) if options.kind_of? Hash
228
- @extend_at_configuration ||= ExtendModelAt::Extention.new({:model => self, :column_name => column_name.to_sym, :columns => columns})
237
+ @extend_at_configuration ||= ExtendModelAt::Extention.new({:model => self, :column_name => column_name.to_sym, :columns => columns, :static => options[:static]})
229
238
  end
230
239
  @extend_at_configuration
231
240
  end
@@ -239,7 +248,8 @@ module ExtendModelAt
239
248
  if validation.kind_of? Symbol
240
249
  self.send validation, eval("@extend_at_configuration.\#\{column.to_s\}", binding)
241
250
  elsif validation.kind_of? Proc
242
- validation.call @extend_at_configuration[column.to_sym]
251
+ instance_exec @extend_at_configuration[column.to_sym], &validation
252
+ # validation.call @extend_at_configuration[column.to_sym]
243
253
  end
244
254
  end
245
255
  end
@@ -257,18 +267,18 @@ module ExtendModelAt
257
267
  end
258
268
  elsif options[:columns].kind_of? Symbol
259
269
  hash = self.send options[:columns]
260
- raise "Invalid columns configuration" if not hash.kind_of? Hash
270
+ raise ExtendModelAt::ArgumentError, "Invalid columns configuration" if not hash.kind_of? Hash
261
271
  columns = initialize_columns :columns => hash
262
272
  elsif options[:columns].kind_of? Proc
263
273
  hash = options[:columns].call
264
- raise "Invalid columns configuration" if not hash.kind_of? Hash
274
+ raise ExtendModelAt::ArgumentError, "Invalid columns configuration" if not hash.kind_of? Hash
265
275
  columns = initialize_columns :columns => hash
266
276
  end
267
277
  columns
268
278
  end
269
279
 
270
280
  def initialize_column(column,config={})
271
- raise "The column \#\{column\} have an invalid configuration (\#\{config.class\} => \#\{config\})" if not config.kind_of? Hash
281
+ raise ExtendModelAt::ArgumentError, "The column \#\{column\} have an invalid configuration (\#\{config.class\} => \#\{config\})" if not config.kind_of? Hash
272
282
 
273
283
  @VALID_SYMBOLS ||= [:any, :binary, :boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp]
274
284
 
@@ -284,7 +294,7 @@ module ExtendModelAt
284
294
  elsif [Symbol, Proc].include? config[:type]
285
295
  column_config[:type] = get_value_of config[:type]
286
296
  else
287
- raise "\#\{config[:type]\} is not a valid column type"
297
+ raise ExtendModelAt::ArgumentError, "\#\{config[:type]\} is not a valid column type"
288
298
  end
289
299
 
290
300
  # Stablish the default value
@@ -297,7 +307,7 @@ module ExtendModelAt
297
307
  # If the column have a type, we verify the type
298
308
  if not column_config[:type].nil?
299
309
  if not valid_type?(config[:default], column_config[:type])
300
- raise "The column \#\{column\} has an invalid default value. Expected \#\{column_config[:type]}, not \#\{config[:default].class}"
310
+ raise ExtendModelAt::ArgumentError, "The column \#\{column\} has an invalid default value. Expected \#\{column_config[:type]}, not \#\{config[:default].class}"
301
311
  end
302
312
  column_config[:default] = config[:default]
303
313
  else
@@ -311,7 +321,7 @@ module ExtendModelAt
311
321
  column_config[:validate] = config[:validate]
312
322
  create_validation_for column, config[:validate]
313
323
  elsif not config[:validate].nil?
314
- raise "The validation of \#\{column\} is invalid"
324
+ raise ExtendModelAt::ArgumentError, "The validation of \#\{column\} is invalid"
315
325
  end
316
326
 
317
327
 
data/spec/app/.gitignore CHANGED
@@ -1,5 +1,4 @@
1
1
  .bundle
2
- db/*.sqlite3
3
2
  log/*.log
4
3
  tmp/
5
4
  .sass-cache/
@@ -8,7 +8,7 @@ class Article < ActiveRecord::Base
8
8
  :type => :get_int1_type,
9
9
  :default => 1,
10
10
  :validate => lambda do |value|
11
- errors.add :extra_int1, "Most by greater than 0" if value <= 0
11
+ self.errors.add :extra_int1, "Most by greater than 0" if value <= 0
12
12
  end
13
13
  },
14
14
  :int2 => :get_int2_config
@@ -12,5 +12,5 @@ class User < ActiveRecord::Base
12
12
  :type => Time
13
13
  }
14
14
  }
15
- end)
15
+ end), :static => true
16
16
  end
@@ -0,0 +1,14 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+
7
+ # Warning: The database defined as "test" will be erased and
8
+ # re-generated from your development database when you run "rake".
9
+ # Do not set this db to the same as development or production.
10
+ test:
11
+ adapter: sqlite3
12
+ database: db/test.sqlite3
13
+ pool: 5
14
+ timeout: 5000
@@ -3,23 +3,19 @@
3
3
  #
4
4
  # Ensure the SQLite 3 gem is defined in your Gemfile
5
5
  # gem 'sqlite3'
6
- development:
7
- adapter: sqlite3
8
- database: db/development.sqlite3
9
- pool: 5
10
- timeout: 5000
11
6
 
12
7
  # Warning: The database defined as "test" will be erased and
13
8
  # re-generated from your development database when you run "rake".
14
9
  # Do not set this db to the same as development or production.
15
- test:
10
+ development:
16
11
  adapter: sqlite3
17
- database: db/test.sqlite3
12
+ database: db/development.sqlite3
18
13
  pool: 5
19
14
  timeout: 5000
20
15
 
21
- production:
16
+
17
+ test:
22
18
  adapter: sqlite3
23
- database: db/production.sqlite3
19
+ database: db/test.sqlite3
24
20
  pool: 5
25
21
  timeout: 5000
File without changes
@@ -56,5 +56,24 @@ describe 'extend_at' do
56
56
  article.extra.int2 = 10
57
57
  article.extra.int2.class.should == Fixnum
58
58
  end
59
+
60
+ it "should accept validate" do
61
+ article = Article.new :extra_int1 => -1
62
+ article.save
63
+ article.errors.keys.include?(:extra_int1).should == true
64
+ end
65
+
66
+ it "should support static columns" do
67
+ user = User.new :name => "Account"
68
+ lambda {user.private_info.etc = "etc"}.should raise_error(ExtendModelAt::InvalidColumn)
69
+ end
70
+
71
+ it "if is't static should support dynamic columns" do
72
+ article = Article.new
73
+ lambda {article.extra.etc = "etc"}.should_not raise_error(ExtendModelAt::InvalidColumn)
74
+ article.save :validate => false
75
+ article.reload
76
+ article.extra.etc.should == "etc"
77
+ end
59
78
  end
60
79
  end
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.1.5
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-16 00:00:00.000000000 Z
12
+ date: 2012-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &18612180 !ruby/object:Gem::Requirement
16
+ requirement: &8578760 !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: *18612180
24
+ version_requirements: *8578760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &18611460 !ruby/object:Gem::Requirement
27
+ requirement: &8578000 !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: *18611460
35
+ version_requirements: *8578000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-core
38
- requirement: &18610920 !ruby/object:Gem::Requirement
38
+ requirement: &8577360 !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: *18610920
46
+ version_requirements: *8577360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: database_cleaner
49
- requirement: &18610180 !ruby/object:Gem::Requirement
49
+ requirement: &8576820 !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: *18610180
57
+ version_requirements: *8576820
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:
@@ -65,7 +65,9 @@ extra_rdoc_files: []
65
65
  files:
66
66
  - .gitignore
67
67
  - .rspec
68
+ - .travis.yml
68
69
  - Gemfile
70
+ - Gemfile.ci
69
71
  - MIT-license.txt
70
72
  - README.markdown
71
73
  - Rakefile
@@ -93,7 +95,6 @@ files:
93
95
  - lib/generators/extend_at/templates/create_extend_at_tables.rb
94
96
  - spec/app/.gitignore
95
97
  - spec/app/Gemfile
96
- - spec/app/Gemfile.lock
97
98
  - spec/app/README
98
99
  - spec/app/Rakefile
99
100
  - spec/app/app/assets/images/rails.png
@@ -128,6 +129,7 @@ files:
128
129
  - spec/app/config.ru
129
130
  - spec/app/config/application.rb
130
131
  - spec/app/config/boot.rb
132
+ - spec/app/config/database.sqlite.yml
131
133
  - spec/app/config/database.yml
132
134
  - spec/app/config/environment.rb
133
135
  - spec/app/config/environments/development.rb
@@ -146,6 +148,7 @@ files:
146
148
  - spec/app/db/migrate/20120110153027_create_extend_at_tables.rb
147
149
  - spec/app/db/schema.rb
148
150
  - spec/app/db/seeds.rb
151
+ - spec/app/db/test.sqlite3
149
152
  - spec/app/doc/README_FOR_APP
150
153
  - spec/app/lib/assets/.gitkeep
151
154
  - spec/app/lib/tasks/.gitkeep
@@ -202,7 +205,6 @@ summary: Create dynamic fields to extend rails models (like content types in Dru
202
205
  test_files:
203
206
  - spec/app/.gitignore
204
207
  - spec/app/Gemfile
205
- - spec/app/Gemfile.lock
206
208
  - spec/app/README
207
209
  - spec/app/Rakefile
208
210
  - spec/app/app/assets/images/rails.png
@@ -237,6 +239,7 @@ test_files:
237
239
  - spec/app/config.ru
238
240
  - spec/app/config/application.rb
239
241
  - spec/app/config/boot.rb
242
+ - spec/app/config/database.sqlite.yml
240
243
  - spec/app/config/database.yml
241
244
  - spec/app/config/environment.rb
242
245
  - spec/app/config/environments/development.rb
@@ -255,6 +258,7 @@ test_files:
255
258
  - spec/app/db/migrate/20120110153027_create_extend_at_tables.rb
256
259
  - spec/app/db/schema.rb
257
260
  - spec/app/db/seeds.rb
261
+ - spec/app/db/test.sqlite3
258
262
  - spec/app/doc/README_FOR_APP
259
263
  - spec/app/lib/assets/.gitkeep
260
264
  - spec/app/lib/tasks/.gitkeep
@@ -1,136 +0,0 @@
1
- PATH
2
- remote: ../../
3
- specs:
4
- extend_at (0.1.4)
5
- rails (~> 3.1)
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- actionmailer (3.1.3)
11
- actionpack (= 3.1.3)
12
- mail (~> 2.3.0)
13
- actionpack (3.1.3)
14
- activemodel (= 3.1.3)
15
- activesupport (= 3.1.3)
16
- builder (~> 3.0.0)
17
- erubis (~> 2.7.0)
18
- i18n (~> 0.6)
19
- rack (~> 1.3.5)
20
- rack-cache (~> 1.1)
21
- rack-mount (~> 0.8.2)
22
- rack-test (~> 0.6.1)
23
- sprockets (~> 2.0.3)
24
- activemodel (3.1.3)
25
- activesupport (= 3.1.3)
26
- builder (~> 3.0.0)
27
- i18n (~> 0.6)
28
- activerecord (3.1.3)
29
- activemodel (= 3.1.3)
30
- activesupport (= 3.1.3)
31
- arel (~> 2.2.1)
32
- tzinfo (~> 0.3.29)
33
- activeresource (3.1.3)
34
- activemodel (= 3.1.3)
35
- activesupport (= 3.1.3)
36
- activesupport (3.1.3)
37
- multi_json (~> 1.0)
38
- ansi (1.4.1)
39
- arel (2.2.1)
40
- builder (3.0.0)
41
- coffee-rails (3.1.1)
42
- coffee-script (>= 2.2.0)
43
- railties (~> 3.1.0)
44
- coffee-script (2.2.0)
45
- coffee-script-source
46
- execjs
47
- coffee-script-source (1.2.0)
48
- diff-lcs (1.1.3)
49
- erubis (2.7.0)
50
- execjs (1.2.13)
51
- multi_json (~> 1.0)
52
- hike (1.2.1)
53
- i18n (0.6.0)
54
- jquery-rails (1.0.19)
55
- railties (~> 3.0)
56
- thor (~> 0.14)
57
- json (1.6.4)
58
- mail (2.3.0)
59
- i18n (>= 0.4.0)
60
- mime-types (~> 1.16)
61
- treetop (~> 1.4.8)
62
- mime-types (1.17.2)
63
- multi_json (1.0.4)
64
- polyglot (0.3.3)
65
- rack (1.3.6)
66
- rack-cache (1.1)
67
- rack (>= 0.4)
68
- rack-mount (0.8.3)
69
- rack (>= 1.0.0)
70
- rack-ssl (1.3.2)
71
- rack
72
- rack-test (0.6.1)
73
- rack (>= 1.0)
74
- rails (3.1.3)
75
- actionmailer (= 3.1.3)
76
- actionpack (= 3.1.3)
77
- activerecord (= 3.1.3)
78
- activeresource (= 3.1.3)
79
- activesupport (= 3.1.3)
80
- bundler (~> 1.0)
81
- railties (= 3.1.3)
82
- railties (3.1.3)
83
- actionpack (= 3.1.3)
84
- activesupport (= 3.1.3)
85
- rack-ssl (~> 1.3.2)
86
- rake (>= 0.8.7)
87
- rdoc (~> 3.4)
88
- thor (~> 0.14.6)
89
- rake (0.9.2.2)
90
- rdoc (3.12)
91
- json (~> 1.4)
92
- rspec (2.8.0)
93
- rspec-core (~> 2.8.0)
94
- rspec-expectations (~> 2.8.0)
95
- rspec-mocks (~> 2.8.0)
96
- rspec-core (2.8.0)
97
- rspec-expectations (2.8.0)
98
- diff-lcs (~> 1.1.2)
99
- rspec-mocks (2.8.0)
100
- sass (3.1.12)
101
- sass-rails (3.1.5)
102
- actionpack (~> 3.1.0)
103
- railties (~> 3.1.0)
104
- sass (~> 3.1.10)
105
- tilt (~> 1.3.2)
106
- sprockets (2.0.3)
107
- hike (~> 1.2)
108
- rack (~> 1.0)
109
- tilt (~> 1.1, != 1.3.0)
110
- sqlite3 (1.3.5)
111
- thor (0.14.6)
112
- tilt (1.3.3)
113
- treetop (1.4.10)
114
- polyglot
115
- polyglot (>= 0.3.1)
116
- turn (0.8.3)
117
- ansi
118
- tzinfo (0.3.31)
119
- uglifier (1.2.1)
120
- execjs (>= 0.3.0)
121
- multi_json (>= 1.0.2)
122
-
123
- PLATFORMS
124
- ruby
125
-
126
- DEPENDENCIES
127
- coffee-rails (~> 3.1)
128
- extend_at!
129
- jquery-rails
130
- rails (~> 3.1)
131
- rspec
132
- rspec-core
133
- sass-rails (~> 3.1)
134
- sqlite3
135
- turn
136
- uglifier