has_defaults 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/test.yml +102 -0
  3. data/.gitignore +2 -1
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +23 -0
  6. data/Gemfile +1 -0
  7. data/Gemfile.3.2.mysql2 +16 -0
  8. data/Gemfile.3.2.mysql2.lock +75 -0
  9. data/Gemfile.4.2.mysql2 +15 -0
  10. data/Gemfile.4.2.mysql2.lock +73 -0
  11. data/Gemfile.4.2.pg +15 -0
  12. data/Gemfile.4.2.pg.lock +73 -0
  13. data/Gemfile.5.2.mysql2 +15 -0
  14. data/Gemfile.5.2.mysql2.lock +71 -0
  15. data/Gemfile.5.2.pg +16 -0
  16. data/Gemfile.5.2.pg.lock +77 -0
  17. data/Gemfile.6.1.pg +15 -0
  18. data/Gemfile.6.1.pg.lock +75 -0
  19. data/Gemfile.lock +1 -0
  20. data/{MIT-LICENSE → LICENSE} +1 -1
  21. data/README.md +70 -0
  22. data/Rakefile +5 -29
  23. data/has_defaults.gemspec +24 -14
  24. data/lib/has_defaults/active_record_ext.rb +27 -6
  25. data/lib/has_defaults/version.rb +1 -1
  26. data/lib/has_defaults.rb +1 -3
  27. metadata +57 -132
  28. data/README +0 -74
  29. data/pkg/has_defaults-0.3.0.gem +0 -0
  30. data/spec/app_root/log/in_memory.log +0 -147
  31. data/spec/rails-2.3/Gemfile +0 -8
  32. data/spec/rails-2.3/Gemfile.lock +0 -51
  33. data/spec/rails-2.3/Rakefile +0 -11
  34. data/spec/rails-2.3/app_root/config/boot.rb +0 -129
  35. data/spec/rails-2.3/app_root/config/database.yml +0 -4
  36. data/spec/rails-2.3/app_root/config/environment.rb +0 -14
  37. data/spec/rails-2.3/app_root/config/environments/test.rb +0 -28
  38. data/spec/rails-2.3/app_root/config/preinitializer.rb +0 -20
  39. data/spec/rails-2.3/app_root/config/routes.rb +0 -4
  40. data/spec/rails-2.3/app_root/lib/console_with_fixtures.rb +0 -4
  41. data/spec/rails-2.3/app_root/log/.gitignore +0 -1
  42. data/spec/rails-2.3/app_root/script/console +0 -7
  43. data/spec/rails-2.3/rcov.opts +0 -2
  44. data/spec/rails-2.3/spec.opts +0 -4
  45. data/spec/rails-2.3/spec_helper.rb +0 -24
  46. data/spec/rails-3.0/.rspec +0 -2
  47. data/spec/rails-3.0/Gemfile +0 -9
  48. data/spec/rails-3.0/Gemfile.lock +0 -101
  49. data/spec/rails-3.0/Rakefile +0 -10
  50. data/spec/rails-3.0/app_root/.gitignore +0 -4
  51. data/spec/rails-3.0/app_root/config/application.rb +0 -32
  52. data/spec/rails-3.0/app_root/config/boot.rb +0 -13
  53. data/spec/rails-3.0/app_root/config/database.yml +0 -4
  54. data/spec/rails-3.0/app_root/config/environment.rb +0 -5
  55. data/spec/rails-3.0/app_root/config/environments/test.rb +0 -35
  56. data/spec/rails-3.0/app_root/config/initializers/backtrace_silencers.rb +0 -7
  57. data/spec/rails-3.0/app_root/config/initializers/inflections.rb +0 -10
  58. data/spec/rails-3.0/app_root/config/initializers/mime_types.rb +0 -5
  59. data/spec/rails-3.0/app_root/config/initializers/secret_token.rb +0 -7
  60. data/spec/rails-3.0/app_root/config/initializers/session_store.rb +0 -8
  61. data/spec/rails-3.0/app_root/config/locales/en.yml +0 -5
  62. data/spec/rails-3.0/app_root/config/routes.rb +0 -58
  63. data/spec/rails-3.0/app_root/lib/tasks/.gitkeep +0 -0
  64. data/spec/rails-3.0/app_root/log/.gitkeep +0 -0
  65. data/spec/rails-3.0/app_root/script/rails +0 -6
  66. data/spec/rails-3.0/log/in_memory.log +0 -33
  67. data/spec/rails-3.0/rcov.opts +0 -2
  68. data/spec/rails-3.0/spec_helper.rb +0 -25
  69. data/spec/rails-3.2/.rspec +0 -2
  70. data/spec/rails-3.2/Gemfile +0 -9
  71. data/spec/rails-3.2/Gemfile.lock +0 -120
  72. data/spec/rails-3.2/Rakefile +0 -10
  73. data/spec/rails-3.2/app_root/.gitignore +0 -4
  74. data/spec/rails-3.2/app_root/config/application.rb +0 -32
  75. data/spec/rails-3.2/app_root/config/boot.rb +0 -13
  76. data/spec/rails-3.2/app_root/config/database.yml +0 -4
  77. data/spec/rails-3.2/app_root/config/environment.rb +0 -5
  78. data/spec/rails-3.2/app_root/config/environments/test.rb +0 -35
  79. data/spec/rails-3.2/app_root/config/initializers/backtrace_silencers.rb +0 -7
  80. data/spec/rails-3.2/app_root/config/initializers/inflections.rb +0 -10
  81. data/spec/rails-3.2/app_root/config/initializers/mime_types.rb +0 -5
  82. data/spec/rails-3.2/app_root/config/initializers/secret_token.rb +0 -7
  83. data/spec/rails-3.2/app_root/config/initializers/session_store.rb +0 -8
  84. data/spec/rails-3.2/app_root/config/locales/en.yml +0 -5
  85. data/spec/rails-3.2/app_root/config/routes.rb +0 -58
  86. data/spec/rails-3.2/app_root/lib/tasks/.gitkeep +0 -0
  87. data/spec/rails-3.2/app_root/log/.gitkeep +0 -0
  88. data/spec/rails-3.2/app_root/script/rails +0 -6
  89. data/spec/rails-3.2/log/in_memory.log +0 -33
  90. data/spec/rails-3.2/rcov.opts +0 -2
  91. data/spec/rails-3.2/spec_helper.rb +0 -25
  92. data/spec/shared/app_root/app/controllers/application_controller.rb +0 -2
  93. data/spec/shared/app_root/app/models/donut.rb +0 -21
  94. data/spec/shared/app_root/app/models/model_without_defaults.rb +0 -2
  95. data/spec/shared/app_root/app/models/pastry.rb +0 -6
  96. data/spec/shared/app_root/db/migrate/001_create_pastries.rb +0 -18
  97. data/spec/shared/app_root/db/migrate/002_create_model_without_defaults.rb +0 -12
  98. data/spec/shared/has_defaults/active_record_ext_spec.rb +0 -84
data/Gemfile.5.2.pg ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Runtime dependencies
4
+ gem 'activerecord'
5
+ gem 'activesupport'
6
+ gem 'pg', '~>0.18.4'
7
+
8
+ # Development dependencies
9
+ gem 'rake'
10
+ gem 'database_cleaner'
11
+ gem 'rspec', '~>3.5'
12
+ gem 'gemika', '>= 0.2.0'
13
+ gem 'pry-byebug'
14
+
15
+ # Gem under test
16
+ gem 'has_defaults', path: '.'
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ has_defaults (1.0.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.6)
11
+ activesupport (= 5.2.6)
12
+ activerecord (5.2.6)
13
+ activemodel (= 5.2.6)
14
+ activesupport (= 5.2.6)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.6)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ byebug (11.1.3)
23
+ coderay (1.1.3)
24
+ concurrent-ruby (1.1.9)
25
+ database_cleaner (2.0.1)
26
+ database_cleaner-active_record (~> 2.0.0)
27
+ database_cleaner-active_record (2.0.1)
28
+ activerecord (>= 5.a)
29
+ database_cleaner-core (~> 2.0.0)
30
+ database_cleaner-core (2.0.1)
31
+ diff-lcs (1.2.5)
32
+ gemika (0.6.1)
33
+ i18n (1.8.10)
34
+ concurrent-ruby (~> 1.0)
35
+ method_source (1.0.0)
36
+ minitest (5.14.4)
37
+ pg (0.18.4)
38
+ pry (0.13.1)
39
+ coderay (~> 1.1)
40
+ method_source (~> 1.0)
41
+ pry-byebug (3.9.0)
42
+ byebug (~> 11.0)
43
+ pry (~> 0.13.0)
44
+ rake (11.3.0)
45
+ rspec (3.5.0)
46
+ rspec-core (~> 3.5.0)
47
+ rspec-expectations (~> 3.5.0)
48
+ rspec-mocks (~> 3.5.0)
49
+ rspec-core (3.5.3)
50
+ rspec-support (~> 3.5.0)
51
+ rspec-expectations (3.5.0)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.5.0)
54
+ rspec-mocks (3.5.0)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.5.0)
57
+ rspec-support (3.5.0)
58
+ thread_safe (0.3.6)
59
+ tzinfo (1.2.9)
60
+ thread_safe (~> 0.1)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ activerecord
67
+ activesupport
68
+ database_cleaner
69
+ gemika (>= 0.2.0)
70
+ has_defaults!
71
+ pg (~> 0.18.4)
72
+ pry-byebug
73
+ rake
74
+ rspec (~> 3.5)
75
+
76
+ BUNDLED WITH
77
+ 2.2.26
data/Gemfile.6.1.pg ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Runtime dependencies
4
+ gem 'activerecord', '~>6.1.3'
5
+ gem 'pg', '~>1.1'
6
+
7
+ # Development dependencies
8
+ gem 'rake'
9
+ gem 'database_cleaner'
10
+ gem 'rspec', '~>3.5'
11
+ gem 'gemika', '>= 0.2.0'
12
+ gem 'pry-byebug'
13
+
14
+ # Gem under test
15
+ gem 'has_defaults', path: '.'
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ has_defaults (1.0.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (6.1.3)
11
+ activesupport (= 6.1.3)
12
+ activerecord (6.1.3)
13
+ activemodel (= 6.1.3)
14
+ activesupport (= 6.1.3)
15
+ activesupport (6.1.3)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ zeitwerk (~> 2.3)
21
+ byebug (11.1.3)
22
+ coderay (1.1.3)
23
+ concurrent-ruby (1.1.8)
24
+ database_cleaner (2.0.1)
25
+ database_cleaner-active_record (~> 2.0.0)
26
+ database_cleaner-active_record (2.0.0)
27
+ activerecord (>= 5.a)
28
+ database_cleaner-core (~> 2.0.0)
29
+ database_cleaner-core (2.0.1)
30
+ diff-lcs (1.4.4)
31
+ gemika (0.5.0)
32
+ i18n (1.8.9)
33
+ concurrent-ruby (~> 1.0)
34
+ method_source (1.0.0)
35
+ minitest (5.14.4)
36
+ pg (1.2.3)
37
+ pry (0.13.1)
38
+ coderay (~> 1.1)
39
+ method_source (~> 1.0)
40
+ pry-byebug (3.9.0)
41
+ byebug (~> 11.0)
42
+ pry (~> 0.13.0)
43
+ rake (13.0.3)
44
+ rspec (3.10.0)
45
+ rspec-core (~> 3.10.0)
46
+ rspec-expectations (~> 3.10.0)
47
+ rspec-mocks (~> 3.10.0)
48
+ rspec-core (3.10.1)
49
+ rspec-support (~> 3.10.0)
50
+ rspec-expectations (3.10.1)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.10.0)
53
+ rspec-mocks (3.10.2)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.10.0)
56
+ rspec-support (3.10.2)
57
+ tzinfo (2.0.4)
58
+ concurrent-ruby (~> 1.0)
59
+ zeitwerk (2.4.2)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ activerecord (~> 6.1.3)
66
+ database_cleaner
67
+ gemika (>= 0.2.0)
68
+ has_defaults!
69
+ pg (~> 1.1)
70
+ pry-byebug
71
+ rake
72
+ rspec (~> 3.5)
73
+
74
+ BUNDLED WITH
75
+ 2.2.26
data/Gemfile.lock ADDED
@@ -0,0 +1 @@
1
+ Gemfile.4.2.mysql2.lock
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008 Nando Vieira
1
+ Copyright (c) 2008-2016 Nando Vieira, with subsequent changes by makandra GmbH
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # has_defaults [![Build Status](https://travis-ci.org/makandra/has_defaults.svg?branch=master)](https://travis-ci.org/makandra/has_defaults)
2
+
3
+ Default values for ActiveRecord models.
4
+
5
+ ## Installation
6
+
7
+ In your `Gemfile`
8
+
9
+ ```
10
+ gem 'has_defaults'
11
+ ```
12
+
13
+ Now run
14
+
15
+ ```
16
+ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Add the method call `has_defaults` to your model.
22
+
23
+ ```
24
+ class Page < ActiveRecord::Base
25
+ has_defaults :title => "New page", :body => "Put your text here"
26
+ end
27
+ ```
28
+
29
+ Attributes will be set only if it's a new record and the attribute is blank.
30
+
31
+ Retrieve the default attribute with the `default_for` instance method:
32
+
33
+ ```
34
+ @page.default_for(:title)
35
+ ```
36
+
37
+ You can pass Proc as attribute:
38
+
39
+ ```
40
+ has_defaults :expires_at => proc { Time.now }
41
+ ```
42
+
43
+ You can override the default attributes as follow:
44
+
45
+ ```
46
+ Page.has_defaults_options = {:title => "Here's your new page", :body => "Write your page text"}
47
+ ```
48
+
49
+ ## Development
50
+
51
+ There are tests in `spec`. We only accept PRs with tests. To run tests:
52
+
53
+ - Install Ruby 2.5.7
54
+ - Create a local test database `has_defaults_test` in both MySQL and PostgreSQL
55
+ - Copy `spec/support/database.sample.yml` to `spec/support/database.yml` and enter your local credentials for the test databases
56
+ - Install development dependencies using `bundle install`
57
+ - Run tests using `bundle exec rspec`
58
+
59
+ We recommend to test large changes against multiple versions of Ruby and multiple dependency sets. We provide some rake tasks to help with this:
60
+
61
+ - Install development dependencies using `rake matrix:install`
62
+ - Run tests using `rake matrix:spec`
63
+
64
+ Note that we have configured Github Actions to automatically run tests in all supported Ruby versions and dependency sets after each push. We will only merge pull requests with successful test runs.
65
+
66
+
67
+ ## Credits
68
+
69
+ * Original version by Nando Vieira (<http://simplesideias.com.br>)
70
+ * Patches in this fork by makandra (<http://makandra.com/>)
data/Rakefile CHANGED
@@ -1,33 +1,9 @@
1
1
  require 'rake'
2
- require 'spec/rake/spectask'
3
2
  require 'bundler/gem_tasks'
4
-
5
- desc 'Default: Run all specs.'
6
- task :default => 'all:spec'
7
-
8
- namespace :all do
9
-
10
- desc "Run specs on all spec apps"
11
- task :spec do
12
- for_each_directory_of('spec/**/Rakefile') do |directory|
13
- env = "SPEC=../../#{ENV['SPEC']} " if ENV['SPEC']
14
- system("cd #{directory} && #{env} bundle exec rake spec")
15
- end
16
- end
17
-
18
- desc "Bundle all spec apps"
19
- task :bundle do
20
- for_each_directory_of('spec/**/Gemfile') do |directory|
21
- system("cd #{directory} && bundle install")
22
- end
23
- end
24
-
3
+ begin
4
+ require 'gemika/tasks'
5
+ rescue LoadError
6
+ puts 'Run `gem install gemika` for additional tasks'
25
7
  end
26
8
 
27
- def for_each_directory_of(path, &block)
28
- Dir[path].sort.each do |rakefile|
29
- directory = File.dirname(rakefile)
30
- puts '', "\033[44m#{directory}\033[0m", ''
31
- block.call(directory)
32
- end
33
- end
9
+ task :default => 'matrix:spec'
data/has_defaults.gemspec CHANGED
@@ -1,19 +1,29 @@
1
- $:.push File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
2
3
  require "has_defaults/version"
3
4
 
4
- Gem::Specification.new do |s|
5
- s.name = 'has_defaults'
6
- s.version = HasDefaults::VERSION
7
- s.authors = ["Henning Koch"]
8
- s.email = 'henning.koch@makandra.de'
9
- s.homepage = 'https://github.com/makandra/has_defaults'
10
- s.summary = 'Default values for ActiveRecord models.'
11
- s.description = s.summary
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'has_defaults'
7
+ spec.version = HasDefaults::VERSION
8
+ spec.required_ruby_version = '>= 2.3.0'
9
+ spec.authors = ['Henning Koch']
10
+ spec.email = ['henning.koch@makandra.de']
12
11
 
13
- s.files = `git ls-files`.split("\n")
14
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
- s.require_paths = ["lib"]
12
+ spec.summary = 'Default values for ActiveRecord models'
13
+ spec.description = 'Default values for ActiveRecord models'
14
+ spec.homepage = 'https://github.com/makandra/has_defaults'
15
+ spec.license = 'MIT'
17
16
 
18
- s.add_dependency('activerecord')
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r(^exe/)) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency('activerecord')
27
+
28
+ # Development dependencies are defined in the Gemfile (therefore no `spec.add_development_dependency` directives)
19
29
  end
@@ -1,27 +1,36 @@
1
1
  module HasDefaults
2
+
3
+ class Error < StandardError; end
4
+
2
5
  module ActiveRecordExt
3
6
 
4
7
  module ClassMethods
5
8
 
6
9
  def has_defaults(attrs)
7
- raise "Hash expected; #{attrs.class} given." unless attrs.is_a?(Hash)
10
+ raise Error, "Hash expected; #{attrs.class} given." unless attrs.is_a?(Hash)
8
11
 
9
12
  include InstanceMethods
10
13
 
14
+ # Check if our parent class had default options, whose accessor we inherited.
15
+ # In this case we clone the default options as to not modify the options of our parent.
11
16
  if respond_to?(:has_defaults_options)
12
17
  self.has_defaults_options = has_defaults_options.dup
13
18
  else
19
+ # Rails 3 and 2 have different copy-and-write accessor generators.
14
20
  if respond_to?(:class_attribute)
15
21
  class_attribute :has_defaults_options
16
22
  else
17
23
  class_inheritable_hash :has_defaults_options
18
24
  end
25
+ # We only register the callback if we haven't registered it before,
26
+ # since in this branch we didn't inherit #has_defaults_options
27
+ after_initialize :set_default_attributes
19
28
  end
20
29
 
21
30
  self.has_defaults_options ||= {}
22
31
  self.has_defaults_options.merge!(attrs)
23
32
 
24
- if Rails.version < '3'
33
+ if ActiveRecord::VERSION::MAJOR < 3
25
34
  # ActiveRecord only calls after_initialize callbacks only if is
26
35
  # explicit defined in a class. We should however only define that
27
36
  # callback if a default has been set, as it really slow downs Rails.
@@ -30,7 +39,6 @@ module HasDefaults
30
39
  end
31
40
  end
32
41
 
33
- after_initialize :set_default_attributes
34
42
  end
35
43
 
36
44
  end
@@ -38,23 +46,36 @@ module HasDefaults
38
46
  module InstanceMethods
39
47
 
40
48
  def default_for(name)
41
- self.class.has_defaults_options[name.to_sym]
49
+ raw_value = self.class.has_defaults_options[name.to_sym]
50
+ evaluate_raw_default_value(raw_value)
42
51
  end
43
52
 
44
53
  private
45
54
 
46
55
  def set_default_attributes
47
56
  if new_record?
48
- self.class.has_defaults_options.each do |name, value|
57
+ self.class.has_defaults_options.each do |name, raw_value|
49
58
  if send(name).nil?
50
- value = instance_eval(&value) if value.respond_to?(:call)
59
+ value = evaluate_raw_default_value(raw_value)
51
60
  send("#{name}=", value)
52
61
  end
53
62
  end
54
63
  end
55
64
  end
56
65
 
66
+ def evaluate_raw_default_value(raw_value)
67
+ value = raw_value
68
+ if value.respond_to?(:call)
69
+ value = instance_exec(&value)
70
+ end
71
+ value
72
+ end
73
+
57
74
  end
58
75
 
59
76
  end
60
77
  end
78
+
79
+ ActiveRecord::Base.extend(HasDefaults::ActiveRecordExt::ClassMethods)
80
+
81
+
@@ -1,5 +1,5 @@
1
1
  module HasDefaults
2
2
 
3
- VERSION = '0.4.0'
3
+ VERSION = '1.0.0'
4
4
 
5
5
  end