active_mocker 1.2 → 1.2.3
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 +4 -4
- data/.gitignore +1 -0
- data/README.md +61 -25
- data/Rakefile +1 -1
- data/active_mocker.gemspec +3 -3
- data/lib/active_hash/init.rb +13 -22
- data/lib/active_mocker.rb +1 -0
- data/lib/active_mocker/active_record/unknown_class_method.rb +1 -1
- data/lib/active_mocker/active_record/unknown_module.rb +3 -3
- data/lib/active_mocker/collection_association.rb +19 -20
- data/lib/active_mocker/config.rb +7 -4
- data/lib/active_mocker/field.rb +4 -4
- data/lib/active_mocker/generate.rb +75 -18
- data/lib/active_mocker/logger.rb +10 -1
- data/lib/active_mocker/mock_class_methods.rb +12 -2
- data/lib/active_mocker/mock_instance_methods.rb +3 -2
- data/lib/active_mocker/mock_requires.rb +2 -1
- data/lib/active_mocker/mock_template.erb +25 -22
- data/lib/active_mocker/public_methods.rb +6 -2
- data/lib/active_mocker/version.rb +1 -1
- data/sample_app_rails_4/app/models/micropost.rb +0 -2
- data/sample_app_rails_4/bin/rspec +16 -0
- data/sample_app_rails_4/config/application.rb +0 -3
- data/sample_app_rails_4/config/database.yml +5 -6
- data/sample_app_rails_4/config/environments/development.rb +0 -2
- data/sample_app_rails_4/config/environments/test.rb +0 -1
- data/sample_app_rails_4/config/initializers/active_mocker.rb +7 -5
- data/sample_app_rails_4/db/migrate/20130311191400_create_users.rb +1 -1
- data/sample_app_rails_4/db/migrate/20130315015932_add_admin_to_users.rb +1 -1
- data/sample_app_rails_4/db/schema.rb +4 -3
- data/sample_app_rails_4/lib/tasks/{mocks.rake → active_mocker.rake} +5 -5
- data/sample_app_rails_4/lib/unit_logger.rb +22 -0
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +110 -4
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +31 -27
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +29 -24
- data/sample_app_rails_4/spec/mocks/user_mock.rb +69 -58
- data/sample_app_rails_4/spec/spec_helper.rb +6 -7
- data/sample_app_rails_4/spec/user_mock_spec.rb +14 -7
- data/spec/lib/active_mocker/collection_association_spec.rb +17 -3
- data/spec/lib/active_mocker/generate_spec.rb +8 -6
- data/spec/lib/active_mocker/model_reader_spec.rb +5 -0
- data/spec/lib/active_mocker/schema_reader_spec.rb +1 -1
- data/spec/lib/readme_spec.rb +199 -205
- data/spec/unit_logger.rb +24 -0
- metadata +22 -32
- data/mocks/micropost_mock.rb +0 -108
- data/mocks/relationship_mock.rb +0 -109
- data/mocks/user_mock.rb +0 -199
- data/plan_mock.rb +0 -2323
- data/sample_app_rails_4/config/cucumber.yml +0 -8
- data/sample_app_rails_4/db/development.sqlite3 +0 -0
- data/sample_app_rails_4/db/test.sqlite3 +0 -0
- data/spec/lib/active_mocker/performance/base_spec.rb +0 -454
- data/spec/lib/active_mocker/performance/large_schema.rb +0 -3576
- data/spec/lib/active_mocker/performance/migration/20140327205359_migration.rb +0 -0
- data/spec/lib/active_mocker/performance/schema_reader_spec.rb +0 -96
- data/spec/lib/compare_mocker_and_record_spec.rb +0 -133
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f014b144505ba0f608261c141298febf45e73a8
|
4
|
+
data.tar.gz: 68c6f7bb51432d351aa977856121b2d78d893f66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0f87e4cf4ea8d106193343d7c7b93444bcf369966d1c0934616c5761235eab288a9421acc0314e7210ae16401f813a51c0999fd28cd8ee09a1853d8b645c091
|
7
|
+
data.tar.gz: 87b90a2801324d5f16bb9adc915c415a67c5ccc832d70970e77781b10c4fc5eab642ed3ff511c60db7d5976628c3b6d554b1d9577e370ab6657da75e9d7e9f1e
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
# ActiveMocker
|
2
2
|
[](https://travis-ci.org/zeisler/active_mocker)
|
3
3
|
|
4
|
-
|
4
|
+
Creates mocks from Active Record models. Allows your test suite to run very fast by not loading Rails or hooking to a database. It parse the schema definition and the definded methods on a model then saves a ruby file that can be included with a test. Mocks are regenerated when the schema is modified so your mocks will not go stale. This prevents the case where your units tests pass but production code is failing.
|
5
5
|
|
6
6
|
Example from a real app
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
Finished in 0.54599 seconds
|
9
|
+
190 examples, 0 failures
|
9
10
|
|
10
11
|
## Installation
|
11
12
|
|
@@ -21,11 +22,37 @@ Or install it yourself as:
|
|
21
22
|
|
22
23
|
$ gem install active_mocker
|
23
24
|
|
24
|
-
## Usage
|
25
25
|
|
26
|
-
|
26
|
+
### Setup
|
27
|
+
config/initializers/active_mocker.rb
|
28
|
+
|
29
|
+
ActiveMocker::Generate.configure do |config|
|
30
|
+
# Required Options
|
31
|
+
config.schema_file = File.join(Rails.root, 'db/schema.rb')
|
32
|
+
config.model_dir = File.join(Rails.root, 'app/models')
|
33
|
+
config.mock_dir = File.join(Rails.root, 'spec/mocks')
|
34
|
+
# Logging
|
35
|
+
config.logger = Rails.logger
|
36
|
+
end
|
37
|
+
|
38
|
+
Here is an example of a rake task to regenerate mocks after every schema modifiation. If the model changes this rake task needs to be called manually. You could add a file watcher for when your models change and have it run the rake task.
|
39
|
+
|
40
|
+
lib/tasks/active_mocker.rake
|
41
|
+
|
42
|
+
task rebuild_mocks: :environment do
|
43
|
+
puts 'rebuilding mocks'
|
44
|
+
ActiveMocker.create_mocks
|
45
|
+
end
|
46
|
+
|
47
|
+
['db:schema:load', 'db:migrate', 'db:reset'].each do |task|
|
48
|
+
Rake::Task[task].enhance do
|
49
|
+
Rake::Task['rebuild_mocks'].invoke
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
## Usage
|
27
54
|
|
28
|
-
|
55
|
+
db/schema.rb
|
29
56
|
|
30
57
|
ActiveRecord::Schema.define(version: 20140327205359) do
|
31
58
|
|
@@ -52,22 +79,11 @@ Needs updating, for now see the rails sample application
|
|
52
79
|
end
|
53
80
|
|
54
81
|
end
|
82
|
+
|
55
83
|
|
56
|
-
|
57
|
-
|
58
|
-
ActiveMocker.configure do |config|
|
59
|
-
# Required Options
|
60
|
-
config.schema_file = "#{APP_ROOT}/db/schema.rb"
|
61
|
-
config.model_dir = "#{APP_ROOT}/app/models"
|
62
|
-
# Additional Options
|
63
|
-
config.schema_attributes = true #default
|
64
|
-
config.model_attributes = true #default
|
65
|
-
# Logging
|
66
|
-
config.log_level = Logger::WARN #default
|
67
|
-
end
|
84
|
+
spec/models/person_spec.rb
|
68
85
|
|
69
|
-
|
70
|
-
=> PersonMock
|
86
|
+
load 'spec/mocks/person_mock.rb'
|
71
87
|
|
72
88
|
PersonMock.column_names
|
73
89
|
=> ["id", "account_id", "first_name", "last_name", "address", "city"]
|
@@ -80,7 +96,7 @@ Needs updating, for now see the rails sample application
|
|
80
96
|
|
81
97
|
### When schema.rb changes, the mock fails
|
82
98
|
|
83
|
-
|
99
|
+
db/schema.rb
|
84
100
|
|
85
101
|
ActiveRecord::Schema.define(version: 20140327205359) do
|
86
102
|
|
@@ -116,7 +132,7 @@ Needs updating, for now see the rails sample application
|
|
116
132
|
|
117
133
|
### When the model changes, the mock fails
|
118
134
|
|
119
|
-
|
135
|
+
app/models/person.rb
|
120
136
|
|
121
137
|
class Person < ActiveRecord::Base
|
122
138
|
belongs_to :account
|
@@ -169,13 +185,33 @@ Needs updating, for now see the rails sample application
|
|
169
185
|
* attributes
|
170
186
|
* update
|
171
187
|
* save
|
172
|
-
* write_attribute - (private)
|
188
|
+
* write_attribute - (private, can be used within an included module)
|
173
189
|
* read_attribute - (private)
|
174
190
|
|
191
|
+
**has_many associations**
|
192
|
+
|
193
|
+
* empty?
|
194
|
+
* length/size/count
|
195
|
+
* uniq
|
196
|
+
* replace
|
197
|
+
* first/last
|
198
|
+
* concat
|
199
|
+
* include
|
200
|
+
* push
|
201
|
+
* clear
|
202
|
+
* take
|
203
|
+
|
204
|
+
**Schema/Migration Option Support**
|
205
|
+
|
206
|
+
* All schema types are supported and on initalization coerced by Virtus. If coercsion fails the passed value will be retained.
|
207
|
+
* Default value
|
208
|
+
|
175
209
|
### Known Limitations
|
176
210
|
|
177
|
-
*
|
178
|
-
* Included/extended module methods will not be included on the mock.
|
211
|
+
* Model names and table names must follow the default ActiveRecord naming pattern.
|
212
|
+
* Included/extended module methods will not be included on the mock. I suggest you keep domain logic out of the model and only add database queries. Domain logic can be put into modules and then included into the mock during test setup.
|
213
|
+
* Deleting one record at a time is not support, this is a limitation of ActiveHash.
|
214
|
+
* Queries will not call other mocks classes, for example when using `where` all attributes must reside inside of each record.
|
179
215
|
|
180
216
|
## Inspiration
|
181
217
|
Thanks to Jeff Olfert for being my original inspiration for this project.
|
data/Rakefile
CHANGED
data/active_mocker.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = ActiveMocker::VERSION
|
9
9
|
spec.authors = ["Dustin Zeisler"]
|
10
10
|
spec.email = ["dustin@zive.me"]
|
11
|
-
spec.summary = %q{
|
12
|
-
spec.description = %q{
|
11
|
+
spec.summary = %q{Creates mocks from Active Record models. Allows your test suite to run very fast by not loading Rails or hooking to a database.}
|
12
|
+
spec.description = %q{Creates mocks from Active Record models. Allows your test suite to run very fast by not loading Rails or hooking to a database. It parse the schema definition and the definded methods on a model then saves a ruby file that can be included with a test. Mocks are regenerated when the schema is modified so your mocks will not go stale. This prevents the case where your units tests pass but production code is failing.}
|
13
13
|
spec.homepage = "https://github.com/zeisler/active_mocker"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.required_ruby_version = '>= 2.0.0'
|
22
22
|
|
23
|
-
spec.add_runtime_dependency "activerecord", "~>4.1"
|
24
23
|
spec.add_runtime_dependency "activesupport", "~>4.1"
|
25
24
|
spec.add_runtime_dependency "active_hash", "~>1.3"
|
25
|
+
spec.add_runtime_dependency "virtus", '~> 1.0'
|
26
26
|
|
27
27
|
spec.add_development_dependency "bundler", "~> 1.5"
|
28
28
|
spec.add_development_dependency "rake", "~>10.1"
|
data/lib/active_hash/init.rb
CHANGED
@@ -1,37 +1,28 @@
|
|
1
1
|
require 'active_support/core_ext/hash/indifferent_access'
|
2
2
|
module ActiveHash
|
3
|
-
|
4
3
|
module ARApi
|
5
4
|
|
6
5
|
module Init
|
7
6
|
|
8
|
-
attr_reader :associations
|
7
|
+
attr_reader :associations, :types
|
9
8
|
|
10
9
|
def initialize(attributes = {})
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
@types = {}
|
11
|
+
attributes.each do |key, value|
|
12
|
+
begin
|
13
|
+
send "#{key}=", value
|
14
|
+
rescue NoMethodError
|
15
|
+
raise ActiveMocker::RejectedParams, "{:#{key}=>#{value.inspect}} for #{self.class.name}"
|
16
|
+
end
|
14
17
|
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def filter_associations(attributes)
|
20
|
-
permitted_params = [*self.class.send(:attribute_names), *self.class.send(:association_names)]
|
21
|
-
rejected_params = attributes.reject{ |p| permitted_params.include? p.to_sym}
|
22
|
-
raise "Rejected params: #{rejected_params} for #{self.class.name}" if !rejected_params.empty?
|
23
|
-
@attributes = attributes.select do |k, v|
|
24
|
-
self.class.send(:attribute_names).include? k.to_sym
|
25
|
-
end
|
26
|
-
@attributes = self.class.send(:attribute_template).merge(@attributes)
|
27
|
-
@associations = attributes.select do |k, v|
|
28
|
-
self.class.send(:association_names).include? k.to_sym
|
29
|
-
end
|
30
|
-
@associations = self.class.send(:association_template).merge(associations)
|
31
18
|
|
32
19
|
end
|
33
|
-
|
34
20
|
end
|
35
21
|
end
|
36
22
|
|
23
|
+
end
|
24
|
+
|
25
|
+
module ActiveMocker
|
26
|
+
class RejectedParams < Exception
|
27
|
+
end
|
37
28
|
end
|
data/lib/active_mocker.rb
CHANGED
@@ -3,7 +3,7 @@ module ActiveMocker
|
|
3
3
|
module UnknownClassMethod
|
4
4
|
|
5
5
|
def method_missing(meth, *args)
|
6
|
-
|
6
|
+
Logger.debug "ActiveMocker :: #{meth} called from class #{self.name} is unknown and will not be available in mock."
|
7
7
|
end
|
8
8
|
|
9
9
|
end
|
@@ -6,8 +6,8 @@ module ActiveMocker
|
|
6
6
|
begin
|
7
7
|
super _module
|
8
8
|
rescue => e
|
9
|
-
|
10
|
-
|
9
|
+
Logger.debug e
|
10
|
+
Logger.debug "ActiveMocker :: Can't include module #{_module} from class #{self.name}.\n #{caller}"
|
11
11
|
|
12
12
|
end
|
13
13
|
end
|
@@ -16,7 +16,7 @@ module ActiveMocker
|
|
16
16
|
begin
|
17
17
|
super _module
|
18
18
|
rescue
|
19
|
-
|
19
|
+
Logger.debug "ActiveMocker :: Can't extend module #{_module} from class #{self.name}..\n #{caller}"
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -4,39 +4,38 @@ module ActiveMocker
|
|
4
4
|
|
5
5
|
include Enumerable
|
6
6
|
|
7
|
-
attr_accessor :collection
|
8
|
-
|
9
7
|
def initialize(collection=[])
|
10
|
-
@
|
11
|
-
end
|
12
|
-
|
13
|
-
def each(&block)
|
14
|
-
collection.each do |item|
|
15
|
-
block.call(item)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def last
|
20
|
-
collection.last
|
8
|
+
@association = [*collection]
|
21
9
|
end
|
22
10
|
|
23
11
|
def <<(*records)
|
24
|
-
|
12
|
+
association.concat(records.flatten)
|
25
13
|
end
|
26
14
|
|
15
|
+
delegate :any?, :empty?, :length, :size, :count, :uniq, :replace, :first, :last, :concat, :include?, :push, :clear, :take, to: :association
|
16
|
+
alias distinct uniq
|
17
|
+
|
27
18
|
def sum(attribute=nil)
|
28
|
-
values =
|
29
|
-
values.inject{ |sum, n| sum + n }
|
19
|
+
values = association.map { |obj| obj.send(attribute) }
|
20
|
+
values.inject { |sum, n| sum + n }
|
30
21
|
end
|
31
22
|
|
32
23
|
def ==(other_ary)
|
33
|
-
|
24
|
+
association == other_ary
|
25
|
+
end
|
26
|
+
|
27
|
+
def each(&block)
|
28
|
+
association.each do |item|
|
29
|
+
block.call(item)
|
30
|
+
end
|
34
31
|
end
|
35
32
|
|
36
|
-
def
|
37
|
-
|
33
|
+
def to_a
|
34
|
+
@association
|
38
35
|
end
|
39
36
|
|
37
|
+
private
|
38
|
+
attr_accessor :association
|
39
|
+
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
data/lib/active_mocker/config.rb
CHANGED
@@ -11,7 +11,9 @@ module ActiveMocker
|
|
11
11
|
:model_file_reader,
|
12
12
|
:clear_cache,
|
13
13
|
:migration_dir,
|
14
|
-
:mock_dir
|
14
|
+
:mock_dir,
|
15
|
+
:logger,
|
16
|
+
:log_level
|
15
17
|
|
16
18
|
def config
|
17
19
|
@@first_load ||= reload_default
|
@@ -20,7 +22,6 @@ module ActiveMocker
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def reload_default
|
23
|
-
@log_level = Logger::WARN
|
24
25
|
@schema_file = nil
|
25
26
|
@model_dir = nil
|
26
27
|
@schema_attributes = true
|
@@ -30,6 +31,7 @@ module ActiveMocker
|
|
30
31
|
@model_file_reader = nil
|
31
32
|
@migration_dir = nil
|
32
33
|
@mock_dir = nil
|
34
|
+
@logger = ::Logger.new(STDOUT)
|
33
35
|
end
|
34
36
|
|
35
37
|
def check_required_settings
|
@@ -38,8 +40,9 @@ module ActiveMocker
|
|
38
40
|
raise 'mock_dir must be specified' if mock_dir.nil?
|
39
41
|
end
|
40
42
|
|
41
|
-
def
|
42
|
-
|
43
|
+
def logger=(logger)
|
44
|
+
@logger = logger
|
45
|
+
Logger.set(logger)
|
43
46
|
end
|
44
47
|
|
45
48
|
end
|
data/lib/active_mocker/field.rb
CHANGED
@@ -2,12 +2,13 @@ module ActiveMocker
|
|
2
2
|
|
3
3
|
class Field
|
4
4
|
|
5
|
-
|
5
|
+
attr_accessor :name, :type, :options
|
6
6
|
|
7
7
|
def initialize(name, type, options)
|
8
8
|
@name = name
|
9
9
|
@type = type
|
10
|
-
@options =
|
10
|
+
@options = {}
|
11
|
+
options.each{|hash| @options[hash.keys.first] = hash.values.first}
|
11
12
|
create_option_methods
|
12
13
|
end
|
13
14
|
|
@@ -18,8 +19,7 @@ module ActiveMocker
|
|
18
19
|
alias_method :to_hash, :to_h
|
19
20
|
|
20
21
|
def create_option_methods
|
21
|
-
options.each do |
|
22
|
-
key, value = opt.first
|
22
|
+
options.each do |key, value|
|
23
23
|
self.instance_variable_set("@#{key}", value)
|
24
24
|
self.class.send(:attr_accessor, key)
|
25
25
|
end
|
@@ -11,7 +11,8 @@ class Generate
|
|
11
11
|
:schema_file,
|
12
12
|
:model_file_reader,
|
13
13
|
:schema_file_reader,
|
14
|
-
:mock_dir
|
14
|
+
:mock_dir,
|
15
|
+
:logger
|
15
16
|
|
16
17
|
def initialize
|
17
18
|
create_template
|
@@ -22,10 +23,10 @@ class Generate
|
|
22
23
|
end
|
23
24
|
|
24
25
|
def self.mock(model_name, force_reload: false)
|
25
|
-
load_mock(model_name
|
26
|
+
load_mock(model_name)
|
26
27
|
end
|
27
28
|
|
28
|
-
def self.load_mock(model_name
|
29
|
+
def self.load_mock(model_name)
|
29
30
|
load File.join(mock_dir, "#{model_name.tableize.singularize}_mock.rb")
|
30
31
|
"#{model_name}Mock".constantize
|
31
32
|
end
|
@@ -53,50 +54,102 @@ class Generate
|
|
53
54
|
end
|
54
55
|
|
55
56
|
def create_template
|
57
|
+
mocks_created = 0
|
56
58
|
tables.each do |table|
|
57
59
|
begin
|
58
60
|
mock_template = MockTemplate.new
|
59
61
|
# Schema Attributes
|
60
62
|
mock_template.class_name = mock_class_name(table.name)
|
61
63
|
mock_template.attribute_names = table.column_names
|
62
|
-
mock_template.attributes = table.
|
64
|
+
mock_template.attributes = field_type_to_class(table.fields)
|
65
|
+
mock_template.default_attributes =default_attr_values(table.fields)
|
63
66
|
|
64
67
|
# Model associations
|
65
68
|
mock_template.single_associations = model_definition(table.name).single_relationships
|
66
69
|
mock_template.collection_associations = model_definition(table.name).collections
|
67
70
|
mock_template.association_names = [*model_definition(table.name).single_relationships, *model_definition(table.name).collections]
|
71
|
+
mock_template.associations = associations(mock_template.association_names)
|
68
72
|
|
69
73
|
# Model Methods
|
70
|
-
mock_template.instance_methods
|
74
|
+
mock_template.instance_methods = instance_methods(table.name).methods
|
71
75
|
mock_template.model_instance_methods = instance_methods(table.name).model_instance_methods
|
72
|
-
mock_template.class_methods
|
76
|
+
mock_template.class_methods = class_methods(table.name).methods
|
73
77
|
mock_template.model_class_methods = class_methods(table.name).model_class_methods
|
74
78
|
|
75
79
|
klass_str = mock_template.render( File.open(File.join(File.expand_path('../', __FILE__), 'mock_template.erb')).read)
|
76
80
|
FileUtils::mkdir_p mock_dir unless File.directory? mock_dir
|
77
81
|
File.open(File.join(mock_dir,"#{table.name.singularize}_mock.rb"), 'w').write(klass_str)
|
78
|
-
|
79
|
-
|
82
|
+
logger.info "saving mock #{table_to_model_file(table.name)} to #{mock_dir}"
|
83
|
+
|
84
|
+
rescue Exception => exception
|
85
|
+
logger.debug $!.backtrace
|
86
|
+
logger.debug exception
|
87
|
+
logger.info "failed to load #{table_to_model_file(table.name)} model"
|
80
88
|
next
|
81
89
|
end
|
90
|
+
mocks_created += 1
|
91
|
+
|
92
|
+
end
|
93
|
+
logger.info "Generated #{mocks_created} of #{tables.count} mocks"
|
94
|
+
end
|
95
|
+
|
96
|
+
def field_type_to_class(fields)
|
97
|
+
fields.map do |field|
|
98
|
+
field.type = case field.type
|
99
|
+
when :integer then
|
100
|
+
Fixnum
|
101
|
+
when :float then
|
102
|
+
Float
|
103
|
+
when :decimal then
|
104
|
+
BigDecimal
|
105
|
+
when :timestamp, :time then
|
106
|
+
Time
|
107
|
+
when :datetime then
|
108
|
+
DateTime
|
109
|
+
when :date then
|
110
|
+
Date
|
111
|
+
when :text, :string, :binary then
|
112
|
+
String
|
113
|
+
when :boolean then
|
114
|
+
::Virtus::Attribute::Boolean
|
115
|
+
end
|
116
|
+
field
|
82
117
|
end
|
83
118
|
|
84
119
|
end
|
85
120
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
121
|
def mock_class_name(table_name)
|
90
122
|
"#{table_to_class_name(table_name)}Mock"
|
91
123
|
end
|
92
124
|
|
125
|
+
def associations(names)
|
126
|
+
hash = {}
|
127
|
+
names.each do |name|
|
128
|
+
hash[name] = nil
|
129
|
+
end
|
130
|
+
hash
|
131
|
+
end
|
132
|
+
|
133
|
+
def default_attr_values(fields)
|
134
|
+
attributes = {}
|
135
|
+
fields.each do |f|
|
136
|
+
if f.options[:default].nil?
|
137
|
+
attributes[f.name] = nil
|
138
|
+
else
|
139
|
+
value = f.default.class == String ? f.default : f.default
|
140
|
+
attributes[f.name] = value
|
141
|
+
end
|
142
|
+
end
|
143
|
+
attributes
|
144
|
+
end
|
145
|
+
|
93
146
|
def instance_methods(table_name)
|
94
147
|
model_instance_methods = []
|
95
148
|
instance_methods = Methods.new
|
96
149
|
instance_methods.methods = model_definition(table_name).instance_methods_with_arguments.map do |method|
|
97
|
-
m = method.keys.first
|
150
|
+
m = method.keys.first.to_s
|
98
151
|
if m == :attributes
|
99
|
-
|
152
|
+
logger.warn "ActiveMocker Depends on the #attributes method. It will not be redefined for the model."
|
100
153
|
next
|
101
154
|
end
|
102
155
|
params = Reparameterize.call(method.values.first)
|
@@ -110,7 +163,8 @@ class Generate
|
|
110
163
|
model_instance_methods << m
|
111
164
|
instance_method
|
112
165
|
end
|
113
|
-
instance_methods.model_instance_methods =
|
166
|
+
instance_methods.model_instance_methods = {}
|
167
|
+
model_instance_methods.each{|meth| instance_methods.model_instance_methods[meth] = :not_implemented }
|
114
168
|
instance_methods
|
115
169
|
end
|
116
170
|
|
@@ -118,7 +172,7 @@ class Generate
|
|
118
172
|
model_class_methods = []
|
119
173
|
class_methods = Methods.new
|
120
174
|
class_methods.methods = model_definition(table_name).class_methods_with_arguments.map do |method|
|
121
|
-
m = method.keys.first
|
175
|
+
m = method.keys.first.to_s
|
122
176
|
params = Reparameterize.call(method.values.first)
|
123
177
|
params_pass = Reparameterize.call(method.values.first, true)
|
124
178
|
|
@@ -129,7 +183,8 @@ class Generate
|
|
129
183
|
model_class_methods << m
|
130
184
|
class_method
|
131
185
|
end
|
132
|
-
class_methods.model_class_methods =
|
186
|
+
class_methods.model_class_methods = {}
|
187
|
+
model_class_methods.each { |meth| class_methods.model_class_methods[meth] = :not_implemented }
|
133
188
|
class_methods
|
134
189
|
end
|
135
190
|
|
@@ -152,10 +207,12 @@ class Generate
|
|
152
207
|
:instance_methods,
|
153
208
|
:class_methods,
|
154
209
|
:model_instance_methods,
|
155
|
-
:model_class_methods
|
210
|
+
:model_class_methods,
|
211
|
+
:default_attributes,
|
212
|
+
:associations
|
156
213
|
|
157
214
|
def render(template)
|
158
|
-
ERB.new(template,nil,
|
215
|
+
ERB.new(template, nil, '-').result(binding)
|
159
216
|
end
|
160
217
|
end
|
161
218
|
|