mongoid-sadstory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00875a0c9d4a1cfd69699fdd3ea66e5cf6001993
4
+ data.tar.gz: f3b1d9dd9ceaee7ab08bcd1dda17e71ad948e5e3
5
+ SHA512:
6
+ metadata.gz: 0a24be3d3e36dbb2099af33facea8cea71f67ef19e8cd747cd9c47f15de1ddfd463d1e14add9c8758e6b3be02fed1ccca685996b80a3405db27084a86ab8b782
7
+ data.tar.gz: a5b1534f58832e64f6b250757d0b6a7ffca32f643ced2ed0ebe7bbe443d0e7ac8f9c11780e108e17a4f2eed87f551d5f3ca0e516df471a431ec821fa5f1e8a27
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-sadstory.gemspec
4
+ gemspec
5
+ gem "mongoid", github: 'mongoid/mongoid'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marcin Stecki
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Mongoid::Sadstory
2
+
3
+ Adds support for multiparameter fields to mongoid 4.x series.
4
+
5
+ # Sad story.
6
+
7
+ This is a sad story - mongoid maintainers decided to drop support for multiparamter fields in mongoid 4.x, leaving it to ActiveSupport/ActiveModel and rails.
8
+
9
+ Sadly there was no extraction of this feature ready after ror 4.x was released and since mongoid 4.x was the only version working with ror 4.x series this meant you could not update your application from ror 3.x to 4.x if you were using mongoid and you had date/time/datetime fields somewhere in your application.
10
+
11
+ That's just sad.
12
+
13
+ What I did is just extracted our hacks to make multiparams working again. Make sure your specs are passing before using this in prod systems...
14
+
15
+ ## Another sad story.
16
+
17
+ If you were using attr_accessible+roles with mongoid you will be 'glad' to hear that's it's alsmost impossible to use it now with 4.x series - since [protected_attributes](https://github.com/rails/protected_attributes) gem adds this feature only for ActiveRecord.
18
+
19
+ With mongoid all you can do is `include ActiveModel::MassAssignmentSecurity` which adds attr_accessible method to mongoid, but does not add roles support (I've tried to hack it through, but there were to many changes in mongoid 3.x and 4.x)
20
+
21
+ ## Last sad story.
22
+
23
+ Making this gem was sad and I'm sad now. I hope to remove it one day, once rails team will manage to extract mulitparams support out of AR to AM and put an end this madness.
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ gem 'mongoid-sadstory'
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install mongoid-sadstory
38
+
39
+
40
+ ## Contributing
41
+
42
+ 1. Furk it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,93 @@
1
+ require "mongoid/sadstory/version"
2
+ require "mongoid"
3
+
4
+ module Mongoid
5
+
6
+ # Your sad story goes here
7
+ # adds support for multiparamter attributes (like dates and times)
8
+ # https://github.com/mongoid/mongoid/issues/2954 copied over as sugested
9
+ # https://github.com/rails/rails/pull/8189
10
+ # and fixed a little (there are more changes between 3.x and 4.x mongoid in regards of paramters handling)
11
+
12
+ module Sadstory
13
+
14
+ module Errors
15
+
16
+ class AttributeAssignmentError < Mongoid::Errors::MongoidError
17
+ attr_reader :exception, :attribute
18
+
19
+ def initialize(message, exception, attribute)
20
+ @exception = exception
21
+ @attribute = attribute
22
+ @message = message
23
+ end
24
+ end
25
+
26
+ class MultiparameterAssignmentErrors < Mongoid::Errors::MongoidError
27
+ attr_reader :errors
28
+
29
+ def initialize(errors)
30
+ @errors = errors
31
+ end
32
+ end
33
+ end
34
+
35
+ def process_attributes(attrs = nil)
36
+ if attrs
37
+ errors = []
38
+ attributes = attrs.class.new
39
+ attributes.permit! if attrs.respond_to?(:permitted?) && attrs.permitted?
40
+ multi_parameter_attributes = {}
41
+ attrs.each_pair do |key, value|
42
+ if key =~ /\A([^\(]+)\((\d+)([if])\)$/
43
+ key, index = $1, $2.to_i
44
+ (multi_parameter_attributes[key] ||= {})[index] = value.empty? ? nil : value.send("to_#{$3}")
45
+ else
46
+ attributes[key] = value
47
+ end
48
+ end
49
+
50
+ multi_parameter_attributes.each_pair do |key, values|
51
+ begin
52
+ values = (values.keys.min..values.keys.max).map { |i| values[i] }
53
+ field = self.class.fields[database_field_name(key)]
54
+ attributes[key] = instantiate_object(field, values)
55
+ rescue => e
56
+ errors << Errors::AttributeAssignmentError.new(
57
+ "error on assignment #{values.inspect} to #{key}", e, key
58
+ )
59
+ end
60
+ end
61
+
62
+ unless errors.empty?
63
+ raise Errors::MultiparameterAssignmentErrors.new(errors),
64
+ "#{errors.size} error(s) on assignment of multiparameter attributes"
65
+ end
66
+
67
+ super attributes
68
+ else
69
+ super
70
+ end
71
+ end
72
+
73
+ protected
74
+
75
+ def instantiate_object(field, values_with_empty_parameters)
76
+ return nil if values_with_empty_parameters.all? { |v| v.nil? }
77
+ values = values_with_empty_parameters.collect { |v| v.nil? ? 1 : v }
78
+ klass = field.type
79
+ if klass == DateTime || klass == Date || klass == Time
80
+ field.mongoize(values)
81
+ elsif klass
82
+ klass.new(*values)
83
+ else
84
+ values
85
+ end
86
+ end
87
+ end
88
+
89
+ module Document
90
+ include Sadstory
91
+ end
92
+
93
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Sadstory
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/sadstory/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-sadstory"
8
+ spec.version = Mongoid::Sadstory::VERSION
9
+ spec.authors = ["Marcin Stecki"]
10
+ spec.email = ["marcin@netguru.pl"]
11
+ spec.summary = %q{Adds support for multiparamter attributes to mongoid 4.x series}
12
+ spec.description = %q{This is a sad story - mongoid maintainers decided to drop support for multi paramter fields in mongoid 4.x, leaving it to ActiveSupport/ActiveModel and rails. Sadly there was no extraction ready after ror 4.x was released and since mongoid 4.x was the only version working with ror 4.x series this meant you could not update your application from ror 3.x to 4.x if you were using mongoid and you had date/time/datetime fields somewhere in your application. That's just sad. What I did is just extracted our hacks to make multiparams working again. Make sure your specs are passing before using this in prod systems...}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.add_dependency "mongoid", "~> 4.0"
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Sadstory do
4
+ let(:dumb_class){ DumbClass = Class.new {
5
+ include Mongoid::Document
6
+ include Mongoid::Timestamps
7
+ } }
8
+
9
+ subject{ dumb_class.new }
10
+
11
+ it "should parse multiparamter attributes " do
12
+ subject.assign_attributes(
13
+ "created_at(1i)" => "2013",
14
+ "created_at(2i)" => "02",
15
+ "created_at(3i)" => "01",
16
+ )
17
+ expect(subject.created_at).to eq(Time.parse("2013-02-01"))
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require 'mongoid/sadstory'
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-sadstory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Stecki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This is a sad story - mongoid maintainers decided to drop support for
70
+ multi paramter fields in mongoid 4.x, leaving it to ActiveSupport/ActiveModel and
71
+ rails. Sadly there was no extraction ready after ror 4.x was released and since
72
+ mongoid 4.x was the only version working with ror 4.x series this meant you could
73
+ not update your application from ror 3.x to 4.x if you were using mongoid and you
74
+ had date/time/datetime fields somewhere in your application. That's just sad. What
75
+ I did is just extracted our hacks to make multiparams working again. Make sure your
76
+ specs are passing before using this in prod systems...
77
+ email:
78
+ - marcin@netguru.pl
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - .gitignore
84
+ - Gemfile
85
+ - LICENSE.txt
86
+ - README.md
87
+ - Rakefile
88
+ - lib/mongoid/sadstory.rb
89
+ - lib/mongoid/sadstory/version.rb
90
+ - mongoid-sadstory.gemspec
91
+ - spec/mongoid/sadstory_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: ''
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Adds support for multiparamter attributes to mongoid 4.x series
117
+ test_files:
118
+ - spec/mongoid/sadstory_spec.rb
119
+ - spec/spec_helper.rb