mark_mapper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.rdoc +39 -0
- data/examples/attr_accessible.rb +24 -0
- data/examples/attr_protected.rb +24 -0
- data/examples/cache_key.rb +26 -0
- data/examples/custom_types.rb +26 -0
- data/examples/identity_map.rb +30 -0
- data/examples/identity_map/automatic.rb +2 -0
- data/examples/keys.rb +42 -0
- data/examples/modifiers/set.rb +27 -0
- data/examples/plugins.rb +40 -0
- data/examples/querying.rb +39 -0
- data/examples/sample_app.rb +43 -0
- data/examples/scopes.rb +56 -0
- data/examples/validating/embedded_docs.rb +31 -0
- data/lib/mark_mapper.rb +125 -0
- data/lib/mark_mapper/config.rb +90 -0
- data/lib/mark_mapper/connection.rb +60 -0
- data/lib/mark_mapper/criteria_hash.rb +194 -0
- data/lib/mark_mapper/document.rb +46 -0
- data/lib/mark_mapper/embedded_document.rb +32 -0
- data/lib/mark_mapper/exceptions.rb +33 -0
- data/lib/mark_mapper/extensions/array.rb +27 -0
- data/lib/mark_mapper/extensions/boolean.rb +45 -0
- data/lib/mark_mapper/extensions/date.rb +29 -0
- data/lib/mark_mapper/extensions/duplicable.rb +86 -0
- data/lib/mark_mapper/extensions/float.rb +18 -0
- data/lib/mark_mapper/extensions/hash.rb +26 -0
- data/lib/mark_mapper/extensions/integer.rb +27 -0
- data/lib/mark_mapper/extensions/kernel.rb +11 -0
- data/lib/mark_mapper/extensions/nil_class.rb +18 -0
- data/lib/mark_mapper/extensions/object.rb +30 -0
- data/lib/mark_mapper/extensions/object_id.rb +18 -0
- data/lib/mark_mapper/extensions/set.rb +20 -0
- data/lib/mark_mapper/extensions/string.rb +31 -0
- data/lib/mark_mapper/extensions/symbol.rb +87 -0
- data/lib/mark_mapper/extensions/time.rb +29 -0
- data/lib/mark_mapper/locale/en.yml +5 -0
- data/lib/mark_mapper/middleware/identity_map.rb +41 -0
- data/lib/mark_mapper/normalizers/criteria_hash_key.rb +17 -0
- data/lib/mark_mapper/normalizers/criteria_hash_value.rb +66 -0
- data/lib/mark_mapper/normalizers/fields_value.rb +26 -0
- data/lib/mark_mapper/normalizers/hash_key.rb +19 -0
- data/lib/mark_mapper/normalizers/integer.rb +19 -0
- data/lib/mark_mapper/normalizers/options_hash_value.rb +83 -0
- data/lib/mark_mapper/normalizers/sort_value.rb +55 -0
- data/lib/mark_mapper/options_hash.rb +103 -0
- data/lib/mark_mapper/pagination.rb +6 -0
- data/lib/mark_mapper/pagination/collection.rb +32 -0
- data/lib/mark_mapper/pagination/paginator.rb +46 -0
- data/lib/mark_mapper/plugins.rb +22 -0
- data/lib/mark_mapper/plugins/accessible.rb +61 -0
- data/lib/mark_mapper/plugins/active_model.rb +18 -0
- data/lib/mark_mapper/plugins/associations.rb +96 -0
- data/lib/mark_mapper/plugins/associations/base.rb +98 -0
- data/lib/mark_mapper/plugins/associations/belongs_to_association.rb +63 -0
- data/lib/mark_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +35 -0
- data/lib/mark_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
- data/lib/mark_mapper/plugins/associations/collection.rb +29 -0
- data/lib/mark_mapper/plugins/associations/embedded_collection.rb +44 -0
- data/lib/mark_mapper/plugins/associations/in_array_proxy.rb +133 -0
- data/lib/mark_mapper/plugins/associations/many_association.rb +63 -0
- data/lib/mark_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mark_mapper/plugins/associations/many_documents_proxy.rb +142 -0
- data/lib/mark_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mark_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mark_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mark_mapper/plugins/associations/one_as_proxy.rb +22 -0
- data/lib/mark_mapper/plugins/associations/one_association.rb +48 -0
- data/lib/mark_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
- data/lib/mark_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
- data/lib/mark_mapper/plugins/associations/one_proxy.rb +95 -0
- data/lib/mark_mapper/plugins/associations/proxy.rb +138 -0
- data/lib/mark_mapper/plugins/associations/single_association.rb +46 -0
- data/lib/mark_mapper/plugins/caching.rb +21 -0
- data/lib/mark_mapper/plugins/callbacks.rb +42 -0
- data/lib/mark_mapper/plugins/clone.rb +24 -0
- data/lib/mark_mapper/plugins/counter_cache.rb +97 -0
- data/lib/mark_mapper/plugins/dirty.rb +61 -0
- data/lib/mark_mapper/plugins/document.rb +41 -0
- data/lib/mark_mapper/plugins/dumpable.rb +22 -0
- data/lib/mark_mapper/plugins/dynamic_querying.rb +45 -0
- data/lib/mark_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mark_mapper/plugins/embedded_callbacks.rb +81 -0
- data/lib/mark_mapper/plugins/embedded_document.rb +53 -0
- data/lib/mark_mapper/plugins/equality.rb +23 -0
- data/lib/mark_mapper/plugins/identity_map.rb +144 -0
- data/lib/mark_mapper/plugins/indexable.rb +86 -0
- data/lib/mark_mapper/plugins/inspect.rb +16 -0
- data/lib/mark_mapper/plugins/keys.rb +470 -0
- data/lib/mark_mapper/plugins/keys/key.rb +134 -0
- data/lib/mark_mapper/plugins/keys/static.rb +45 -0
- data/lib/mark_mapper/plugins/logger.rb +18 -0
- data/lib/mark_mapper/plugins/modifiers.rb +140 -0
- data/lib/mark_mapper/plugins/pagination.rb +16 -0
- data/lib/mark_mapper/plugins/partial_updates.rb +77 -0
- data/lib/mark_mapper/plugins/persistence.rb +79 -0
- data/lib/mark_mapper/plugins/protected.rb +45 -0
- data/lib/mark_mapper/plugins/querying.rb +173 -0
- data/lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb +75 -0
- data/lib/mark_mapper/plugins/rails.rb +79 -0
- data/lib/mark_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
- data/lib/mark_mapper/plugins/sci.rb +82 -0
- data/lib/mark_mapper/plugins/scopes.rb +28 -0
- data/lib/mark_mapper/plugins/serialization.rb +109 -0
- data/lib/mark_mapper/plugins/timestamps.rb +29 -0
- data/lib/mark_mapper/plugins/touch.rb +18 -0
- data/lib/mark_mapper/plugins/userstamps.rb +18 -0
- data/lib/mark_mapper/plugins/validations.rb +96 -0
- data/lib/mark_mapper/query.rb +278 -0
- data/lib/mark_mapper/railtie.rb +52 -0
- data/lib/mark_mapper/railtie/database.rake +65 -0
- data/lib/mark_mapper/translation.rb +10 -0
- data/lib/mark_mapper/version.rb +4 -0
- data/lib/rails/generators/mark_mapper/config/config_generator.rb +37 -0
- data/lib/rails/generators/mark_mapper/config/templates/marklogic.yml +19 -0
- data/lib/rails/generators/mark_mapper/model/model_generator.rb +40 -0
- data/lib/rails/generators/mark_mapper/model/templates/model.rb +17 -0
- data/spec/config/mark_mapper.yml +6 -0
- data/spec/examples_spec.rb +25 -0
- data/spec/functional/accessible_spec.rb +198 -0
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +64 -0
- data/spec/functional/associations/belongs_to_proxy_spec.rb +255 -0
- data/spec/functional/associations/in_array_proxy_spec.rb +349 -0
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +230 -0
- data/spec/functional/associations/many_documents_proxy_spec.rb +968 -0
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +238 -0
- data/spec/functional/associations/many_embedded_proxy_spec.rb +288 -0
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +302 -0
- data/spec/functional/associations/one_as_proxy_spec.rb +489 -0
- data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +207 -0
- data/spec/functional/associations/one_embedded_proxy_spec.rb +100 -0
- data/spec/functional/associations/one_proxy_spec.rb +406 -0
- data/spec/functional/associations_spec.rb +48 -0
- data/spec/functional/caching_spec.rb +75 -0
- data/spec/functional/callbacks_spec.rb +330 -0
- data/spec/functional/counter_cache_spec.rb +235 -0
- data/spec/functional/dirty_spec.rb +316 -0
- data/spec/functional/document_spec.rb +310 -0
- data/spec/functional/dumpable_spec.rb +24 -0
- data/spec/functional/dynamic_querying_spec.rb +75 -0
- data/spec/functional/embedded_document_spec.rb +316 -0
- data/spec/functional/equality_spec.rb +20 -0
- data/spec/functional/extensions_spec.rb +16 -0
- data/spec/functional/identity_map_spec.rb +483 -0
- data/spec/functional/keys_spec.rb +339 -0
- data/spec/functional/logger_spec.rb +20 -0
- data/spec/functional/modifiers_spec.rb +446 -0
- data/spec/functional/options_hash_spec.rb +41 -0
- data/spec/functional/pagination_spec.rb +89 -0
- data/spec/functional/partial_updates_spec.rb +530 -0
- data/spec/functional/protected_spec.rb +199 -0
- data/spec/functional/querying_spec.rb +984 -0
- data/spec/functional/rails_spec.rb +55 -0
- data/spec/functional/sci_spec.rb +374 -0
- data/spec/functional/scopes_spec.rb +204 -0
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/timestamps_spec.rb +97 -0
- data/spec/functional/touch_spec.rb +125 -0
- data/spec/functional/userstamps_spec.rb +46 -0
- data/spec/functional/validations_spec.rb +416 -0
- data/spec/quality_spec.rb +51 -0
- data/spec/spec_helper.rb +150 -0
- data/spec/support/matchers.rb +15 -0
- data/spec/support/models.rb +256 -0
- data/spec/symbol_operator_spec.rb +70 -0
- data/spec/symbol_spec.rb +9 -0
- data/spec/unit/associations/base_spec.rb +146 -0
- data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
- data/spec/unit/associations/many_association_spec.rb +64 -0
- data/spec/unit/associations/one_association_spec.rb +48 -0
- data/spec/unit/associations/proxy_spec.rb +103 -0
- data/spec/unit/clone_spec.rb +79 -0
- data/spec/unit/config_generator_spec.rb +24 -0
- data/spec/unit/criteria_hash_spec.rb +218 -0
- data/spec/unit/document_spec.rb +251 -0
- data/spec/unit/dynamic_finder_spec.rb +125 -0
- data/spec/unit/embedded_document_spec.rb +676 -0
- data/spec/unit/equality_spec.rb +38 -0
- data/spec/unit/exceptions_spec.rb +12 -0
- data/spec/unit/extensions_spec.rb +368 -0
- data/spec/unit/identity_map_middleware_spec.rb +134 -0
- data/spec/unit/inspect_spec.rb +47 -0
- data/spec/unit/key_spec.rb +276 -0
- data/spec/unit/keys_spec.rb +155 -0
- data/spec/unit/mark_mapper_spec.rb +37 -0
- data/spec/unit/model_generator_spec.rb +45 -0
- data/spec/unit/normalizers/criteria_hash_key_spec.rb +37 -0
- data/spec/unit/normalizers/criteria_hash_value_spec.rb +200 -0
- data/spec/unit/normalizers/fields_value_spec.rb +45 -0
- data/spec/unit/normalizers/hash_key_spec.rb +15 -0
- data/spec/unit/normalizers/integer_spec.rb +24 -0
- data/spec/unit/normalizers/options_hash_value_spec.rb +99 -0
- data/spec/unit/normalizers/sort_value_spec.rb +98 -0
- data/spec/unit/options_hash_spec.rb +64 -0
- data/spec/unit/pagination/collection_spec.rb +30 -0
- data/spec/unit/pagination/paginator_spec.rb +118 -0
- data/spec/unit/pagination_spec.rb +11 -0
- data/spec/unit/plugins_spec.rb +89 -0
- data/spec/unit/query_spec.rb +837 -0
- data/spec/unit/rails_compatibility_spec.rb +40 -0
- data/spec/unit/rails_reflect_on_association_spec.rb +118 -0
- data/spec/unit/rails_spec.rb +188 -0
- data/spec/unit/serialization_spec.rb +169 -0
- data/spec/unit/serializers/json_serializer_spec.rb +218 -0
- data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
- data/spec/unit/time_zones_spec.rb +44 -0
- data/spec/unit/translation_spec.rb +27 -0
- data/spec/unit/validations_spec.rb +588 -0
- metadata +307 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
require "mark_mapper"
|
2
|
+
require "rails"
|
3
|
+
require "active_model/railtie"
|
4
|
+
|
5
|
+
# Need the action_dispatch railtie to have action_dispatch.rescu_responses initialized correctly
|
6
|
+
require "action_dispatch/railtie"
|
7
|
+
|
8
|
+
module MarkMapper
|
9
|
+
# = MarkMapper Railtie
|
10
|
+
class Railtie < Rails::Railtie
|
11
|
+
|
12
|
+
config.mark_mapper = ActiveSupport::OrderedOptions.new
|
13
|
+
|
14
|
+
# Rescue responses similar to ActiveRecord.
|
15
|
+
# For rails 3.0 and 3.1
|
16
|
+
if Rails.version < "3.2"
|
17
|
+
ActionDispatch::ShowExceptions.rescue_responses['MarkMapper::DocumentNotFound'] = :not_found
|
18
|
+
ActionDispatch::ShowExceptions.rescue_responses['MarkMapper::InvalidKey'] = :unprocessable_entity
|
19
|
+
ActionDispatch::ShowExceptions.rescue_responses['MarkMapper::InvalidScheme'] = :unprocessable_entity
|
20
|
+
ActionDispatch::ShowExceptions.rescue_responses['MarkMapper::NotSupported'] = :unprocessable_entity
|
21
|
+
else
|
22
|
+
# For rails 3.2 and 4.0
|
23
|
+
config.action_dispatch.rescue_responses.merge!(
|
24
|
+
'MarkMapper::DocumentNotFound' => :not_found,
|
25
|
+
'MarkMapper::InvalidKey' => :unprocessable_entity,
|
26
|
+
'MarkMapper::InvalidScheme' => :unprocessable_entity,
|
27
|
+
'MarkMapper::NotSupported' => :unprocessable_entity
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
rake_tasks do
|
32
|
+
load "mark_mapper/railtie/database.rake"
|
33
|
+
end
|
34
|
+
|
35
|
+
initializer "mark_mapper.set_configs" do |app|
|
36
|
+
ActiveSupport.on_load(:mark_mapper) do
|
37
|
+
app.config.mark_mapper.each do |k,v|
|
38
|
+
send "#{k}=", v
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# This sets the database configuration and establishes the connection.
|
44
|
+
initializer "mark_mapper.initialize_database" do |app|
|
45
|
+
config_file = Rails.root.join('config/marklogic.yml')
|
46
|
+
if config_file.file?
|
47
|
+
config = YAML.load(ERB.new(config_file.read).result)
|
48
|
+
MarkMapper.setup(config, Rails.env, :logger => Rails.logger)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
namespace :db do
|
2
|
+
unless Rake::Task.task_defined?("db:drop")
|
3
|
+
desc 'Drops all the collections for the database for the current Rails.env'
|
4
|
+
task :drop => :environment do
|
5
|
+
MarkMapper.database.collections.select {|c| c.name !~ /\Asystem\./ }.each(&:drop)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
unless Rake::Task.task_defined?("db:seed")
|
10
|
+
# if another ORM has defined db:seed, don't run it twice.
|
11
|
+
desc 'Load the seed data from db/seeds.rb'
|
12
|
+
task :seed => :environment do
|
13
|
+
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
|
14
|
+
load(seed_file) if File.exist?(seed_file)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
unless Rake::Task.task_defined?("db:setup")
|
19
|
+
desc 'Create the database, and initialize with the seed data'
|
20
|
+
task :setup => [ 'db:create', 'db:seed' ]
|
21
|
+
end
|
22
|
+
|
23
|
+
unless Rake::Task.task_defined?("db:reseed")
|
24
|
+
desc 'Delete data and seed'
|
25
|
+
task :reseed => [ 'db:drop', 'db:seed' ]
|
26
|
+
end
|
27
|
+
|
28
|
+
unless Rake::Task.task_defined?("db:create")
|
29
|
+
task :create => :environment do
|
30
|
+
# noop
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
unless Rake::Task.task_defined?("db:migrate")
|
35
|
+
task :migrate => :environment do
|
36
|
+
# noop
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
unless Rake::Task.task_defined?("db:schema:load")
|
41
|
+
namespace :schema do
|
42
|
+
task :load do
|
43
|
+
# noop
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
unless Rake::Task.task_defined?("db:test:prepare")
|
49
|
+
namespace :test do
|
50
|
+
task :prepare => :environment do
|
51
|
+
MarkMapper.connect('test')
|
52
|
+
MarkMapper.database.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
53
|
+
MarkMapper.connect(Rails.env)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
desc 'Load indexes from db/indexes.rb'
|
59
|
+
task :index => :environment do
|
60
|
+
indexes = File.join(Rails.root, 'db', 'indexes.rb')
|
61
|
+
load(indexes) if File.exist?(indexes)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
task 'test:prepare' => 'db:test:prepare'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module MarkMapper
|
4
|
+
module Generators
|
5
|
+
class ConfigGenerator < Rails::Generators::Base
|
6
|
+
desc 'creates the MarkMapper configuration at config/marklogic.yml'
|
7
|
+
|
8
|
+
argument :database_name, :type => :string, :optional => true
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_config_file
|
15
|
+
template 'marklogic.yml', File.join('config', 'marklogic.yml')
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def app_name
|
21
|
+
@app_name ||= defined_app_const_base? ? defined_app_name : File.basename(destination_root)
|
22
|
+
end
|
23
|
+
|
24
|
+
def defined_app_name
|
25
|
+
defined_app_const_base.underscore
|
26
|
+
end
|
27
|
+
|
28
|
+
def defined_app_const_base
|
29
|
+
Rails.respond_to?(:application) && defined?(Rails::Application) &&
|
30
|
+
Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, '')
|
31
|
+
end
|
32
|
+
|
33
|
+
alias :defined_app_const_base? :defined_app_const_base
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
defaults: &defaults
|
2
|
+
host: 127.0.0.1
|
3
|
+
port: 27017
|
4
|
+
|
5
|
+
development:
|
6
|
+
<<: *defaults
|
7
|
+
database: <%= database_name || app_name %>_development
|
8
|
+
|
9
|
+
test:
|
10
|
+
<<: *defaults
|
11
|
+
database: <%= database_name || app_name %>_test
|
12
|
+
w: 0
|
13
|
+
|
14
|
+
# set these environment variables on your prod server
|
15
|
+
production:
|
16
|
+
<<: *defaults
|
17
|
+
database: <%= database_name || app_name %>
|
18
|
+
username: <%%= ENV['MARKLOGIC_USERNAME'] %>
|
19
|
+
password: <%%= ENV['MARKLOGIC_PASSWORD'] %>
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module MarkMapper
|
4
|
+
module Generators
|
5
|
+
class ModelGenerator < Rails::Generators::NamedBase
|
6
|
+
desc 'Creates a markmapper model'
|
7
|
+
|
8
|
+
argument :name, :type => :string
|
9
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
10
|
+
|
11
|
+
check_class_collision
|
12
|
+
|
13
|
+
class_option :timestamps, :type => :boolean
|
14
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
|
15
|
+
|
16
|
+
def self.source_root
|
17
|
+
@source_root ||= File.expand_path("../templates", __FILE__)
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_model_file
|
21
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
22
|
+
end
|
23
|
+
|
24
|
+
hook_for :test_framework
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def parent_class_name
|
29
|
+
options[:parent]
|
30
|
+
end
|
31
|
+
|
32
|
+
# Rails 3.0.X compatibility
|
33
|
+
unless methods.include?(:module_namespacing)
|
34
|
+
def module_namespacing(&block)
|
35
|
+
yield if block
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<%- module_namespacing do -%>
|
2
|
+
<%- if parent_class_name.present? -%>
|
3
|
+
class <%= class_name %> < <%= parent_class_name.classify %>
|
4
|
+
<%- else -%>
|
5
|
+
class <%= class_name %>
|
6
|
+
include MarkMapper::Document
|
7
|
+
<%- end -%>
|
8
|
+
|
9
|
+
<%- attributes.each do |attribute| -%>
|
10
|
+
key :<%= attribute.name %>, <%= attribute.type.to_s.camelcase %>
|
11
|
+
<% end -%>
|
12
|
+
<%- if options[:timestamps] -%>
|
13
|
+
timestamps!
|
14
|
+
<%- end -%>
|
15
|
+
|
16
|
+
end
|
17
|
+
<%- end -%>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
describe "The Examples" do
|
2
|
+
|
3
|
+
before do
|
4
|
+
@previous = MarkMapper::Plugins::IdentityMap.enabled
|
5
|
+
end
|
6
|
+
|
7
|
+
after do
|
8
|
+
MarkMapper::Plugins::IdentityMap.enabled = @previous
|
9
|
+
end
|
10
|
+
|
11
|
+
file = File.dirname(__FILE__) + "/../examples/**/*.rb"
|
12
|
+
puts file
|
13
|
+
Dir[file].each do |f|
|
14
|
+
it "should run #{f}" do
|
15
|
+
|
16
|
+
expect {
|
17
|
+
|
18
|
+
Object.send(:remove_const, :User) if Object.constants.include?(:User)
|
19
|
+
|
20
|
+
require f
|
21
|
+
}.to_not raise_error
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Accessible" do
|
4
|
+
context 'A document with accessible attributes' do
|
5
|
+
before do
|
6
|
+
@doc_class = Doc do
|
7
|
+
key :name, String
|
8
|
+
key :admin, Boolean, :default => false
|
9
|
+
|
10
|
+
attr_accessible :name
|
11
|
+
end
|
12
|
+
|
13
|
+
@doc = @doc_class.create(:name => 'Steve Sloan')
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should have accessible attributes class method' do
|
17
|
+
@doc_class.accessible_attributes.should == [:name].to_set
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should default accessible attributes to nil" do
|
21
|
+
Doc().accessible_attributes.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should have accessible_attributes instance method" do
|
25
|
+
@doc.accessible_attributes.should equal(@doc_class.accessible_attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise error if there are protected attributes" do
|
29
|
+
doc = Doc('Post')
|
30
|
+
doc.attr_protected :admin
|
31
|
+
lambda { doc.attr_accessible :name }.
|
32
|
+
should raise_error(/Declare either attr_protected or attr_accessible for Post/)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should know if using accessible attributes" do
|
36
|
+
@doc_class.accessible_attributes?.should be(true)
|
37
|
+
Doc().accessible_attributes?.should be(false)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should assign inaccessible attribute through accessor" do
|
41
|
+
@doc.admin = true
|
42
|
+
@doc.admin.should be_truthy
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should ignore inaccessible attribute on #initialize" do
|
46
|
+
doc = @doc_class.new(:name => 'John', :admin => true)
|
47
|
+
doc.admin.should be_falsey
|
48
|
+
doc.name.should == 'John'
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should not ignore inaccessible attributes on #initialize from the database" do
|
52
|
+
doc = @doc_class.new(:name => 'John')
|
53
|
+
doc.admin = true
|
54
|
+
doc.save!
|
55
|
+
|
56
|
+
doc = @doc_class.first(:name => 'John')
|
57
|
+
doc.admin.should be_truthy
|
58
|
+
doc.name.should == 'John'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not ignore inaccessible attributes on #reload" do
|
62
|
+
doc = @doc_class.new(:name => 'John')
|
63
|
+
doc.admin = true
|
64
|
+
doc.save!
|
65
|
+
|
66
|
+
doc.reload
|
67
|
+
doc.admin.should be_truthy
|
68
|
+
doc.name.should == 'John'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should not ignore inaccessible attribute on #update_attribute" do
|
72
|
+
@doc.update_attribute('admin', true)
|
73
|
+
@doc.admin.should be_truthy
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should ignore inaccessible attribute on #update_attributes" do
|
77
|
+
@doc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
78
|
+
@doc.name.should == 'Ren Hoek'
|
79
|
+
@doc.admin.should be_falsey
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should ignore inaccessible attribute on #update_attributes!" do
|
83
|
+
@doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
84
|
+
@doc.name.should == 'Stimpson J. Cat'
|
85
|
+
@doc.admin.should be_falsey
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should ignore inaccessible attribute on #attributes=" do
|
89
|
+
@doc.attributes = {:name => 'Ren Hoek', :admin => true}
|
90
|
+
@doc.name.should == 'Ren Hoek'
|
91
|
+
@doc.admin.should be_falsey
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should be indifferent to whether the accessible keys are strings or symbols" do
|
95
|
+
@doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
|
96
|
+
@doc.name.should == 'Stimpson J. Cat'
|
97
|
+
@doc.admin.should be_falsey
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should accept nil as constructor's argument without raising exception" do
|
101
|
+
lambda { @doc_class.new(nil) }.should_not raise_error
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should ignore all attributes if called with no args" do
|
105
|
+
@doc_class = Doc do
|
106
|
+
key :name
|
107
|
+
attr_accessible
|
108
|
+
end
|
109
|
+
|
110
|
+
@doc_class.new(:name => 'Steve Sloan').name.should be_nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "Single collection inherited accessible attributes" do
|
115
|
+
before do
|
116
|
+
class ::GrandParent
|
117
|
+
include MarkMapper::Document
|
118
|
+
attr_accessible :name
|
119
|
+
key :name, String
|
120
|
+
key :site_id, ObjectId
|
121
|
+
end
|
122
|
+
GrandParent.collection.remove
|
123
|
+
|
124
|
+
class ::Child < ::GrandParent
|
125
|
+
attr_accessible :position
|
126
|
+
key :position, Integer
|
127
|
+
end
|
128
|
+
|
129
|
+
class ::GrandChild < ::Child; end
|
130
|
+
|
131
|
+
class ::OtherChild < ::GrandParent
|
132
|
+
attr_accessible :favorite_color
|
133
|
+
key :favorite_color, String
|
134
|
+
key :blog_id, ObjectId
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
after do
|
139
|
+
Object.send :remove_const, 'GrandParent' if defined?(::GrandParent)
|
140
|
+
Object.send :remove_const, 'Child' if defined?(::Child)
|
141
|
+
Object.send :remove_const, 'GrandChild' if defined?(::GrandChild)
|
142
|
+
Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should share keys down the inheritance trail" do
|
146
|
+
GrandParent.accessible_attributes.should == [:name].to_set
|
147
|
+
Child.accessible_attributes.should == [:name, :position].to_set
|
148
|
+
GrandChild.accessible_attributes.should == [:name, :position].to_set
|
149
|
+
OtherChild.accessible_attributes.should == [:name, :favorite_color].to_set
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
context "An embedded document with accessible attributes" do
|
154
|
+
before do
|
155
|
+
@doc_class = Doc('Project')
|
156
|
+
@edoc_class = EDoc('Person') do
|
157
|
+
key :name, String
|
158
|
+
key :admin, Boolean, :default => false
|
159
|
+
|
160
|
+
attr_accessible :name
|
161
|
+
end
|
162
|
+
@doc_class.many :people, :class => @edoc_class
|
163
|
+
|
164
|
+
@doc = @doc_class.create(:title => 'MarkMapper')
|
165
|
+
@edoc = @edoc_class.new(:name => 'Steve Sloan')
|
166
|
+
@doc.people << @edoc
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should have accessible attributes class method" do
|
170
|
+
@edoc_class.accessible_attributes.should == [:name].to_set
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should default accessible attributes to nil" do
|
174
|
+
EDoc().accessible_attributes.should be_nil
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should have accessible attributes instance method" do
|
178
|
+
@edoc.accessible_attributes.should equal(@edoc_class.accessible_attributes)
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should assign inaccessible attribute through accessor" do
|
182
|
+
@edoc.admin = true
|
183
|
+
@edoc.admin.should be_truthy
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should ignore inaccessible attribute on #update_attributes" do
|
187
|
+
@edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
188
|
+
@edoc.name.should == 'Ren Hoek'
|
189
|
+
@edoc.admin.should be_falsey
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should ignore inaccessible attribute on #update_attributes!" do
|
193
|
+
@edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
194
|
+
@edoc.name.should == 'Stimpson J. Cat'
|
195
|
+
@edoc.admin.should be_falsey
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|