madoka 0.0.1
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 +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +9 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +2 -0
- data/lib/active_model/model.rb +22 -0
- data/lib/madoka/model.rb +40 -0
- data/lib/madoka/model_invalid.rb +10 -0
- data/lib/madoka/version.rb +3 -0
- data/lib/madoka.rb +6 -0
- data/madoka.gemspec +27 -0
- data/spec/madoka/model_spec.rb +88 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/active_model.rb +19 -0
- metadata +199 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 ayaya
|
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,55 @@
|
|
1
|
+
# Madoka
|
2
|
+
|
3
|
+
A model based on ActiveModel that provides mass assignment with role support.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'madoka'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install madoka
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
An example from spec:
|
22
|
+
|
23
|
+
class MadokaModel
|
24
|
+
include Madoka::Model
|
25
|
+
attr_accessor :accessible_by_default, :protected_by_default
|
26
|
+
attr_accessor :accessible_by_admin
|
27
|
+
attr_accessible :accessible_by_default
|
28
|
+
attr_protected :protected_by_default
|
29
|
+
attr_accessible :accessible_by_admin, as: :admin
|
30
|
+
|
31
|
+
attr_accessor :flow
|
32
|
+
|
33
|
+
before_save :run_before_save
|
34
|
+
after_save :run_after_save
|
35
|
+
|
36
|
+
def run_before_save
|
37
|
+
@flow = [:before_save]
|
38
|
+
end
|
39
|
+
|
40
|
+
def run_after_save
|
41
|
+
@flow << :after_save
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
## Todo
|
46
|
+
|
47
|
+
* Work with multiparameter
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Model
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
extend ActiveModel::Naming
|
6
|
+
extend ActiveModel::Translation
|
7
|
+
include ActiveModel::Validations
|
8
|
+
include ActiveModel::Conversion
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(params={})
|
13
|
+
params.each do |attr, value|
|
14
|
+
self.public_send("#{attr}=", value)
|
15
|
+
end if params
|
16
|
+
end
|
17
|
+
|
18
|
+
def persisted?
|
19
|
+
false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/madoka/model.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_model/model' unless defined?(ActiveModel::Model)
|
3
|
+
require 'active_record/attribute_assignment'
|
4
|
+
require 'active_record/errors'
|
5
|
+
|
6
|
+
module Madoka
|
7
|
+
module Model
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
include ActiveModel::Model
|
10
|
+
include ActiveRecord::AttributeAssignment
|
11
|
+
|
12
|
+
included do
|
13
|
+
extend ActiveModel::Callbacks
|
14
|
+
define_model_callbacks :save
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(params = nil, options = {})
|
18
|
+
assign_attributes(params, options.slice(:as)) if params
|
19
|
+
end
|
20
|
+
|
21
|
+
def save
|
22
|
+
result = valid?
|
23
|
+
run_callbacks(:save) { result }
|
24
|
+
end
|
25
|
+
|
26
|
+
def save!
|
27
|
+
result = valid?
|
28
|
+
run_callbacks(:save) { result or raise(ModelInvalid.new(self)) }
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
protected
|
33
|
+
|
34
|
+
# we don't have primary_key and inheritance_column...
|
35
|
+
def attributes_protected_by_default
|
36
|
+
[]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Madoka
|
2
|
+
class ModelInvalid < StandardError
|
3
|
+
attr_reader :model
|
4
|
+
def initialize(model)
|
5
|
+
@model = model
|
6
|
+
errors = @model.errors.full_messages.join(", ")
|
7
|
+
super(I18n.t("activerecord.errors.messages.record_invalid", :errors => errors))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/madoka.rb
ADDED
data/madoka.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/madoka/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["ayaya"]
|
6
|
+
gem.email = ["ayaya@ayaya.tw"]
|
7
|
+
gem.description = %q{A model based on ActiveModel that provides mass assignment with role support.}
|
8
|
+
gem.summary = %q{A model based on ActiveModel that provides mass assignment with role support.}
|
9
|
+
gem.homepage = "https://github.com/ayamomiji/madoka"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "madoka"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Madoka::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'activemodel'
|
19
|
+
gem.add_dependency 'activerecord'
|
20
|
+
gem.add_dependency 'activesupport'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
gem.add_development_dependency 'rspec', '~> 2.5'
|
24
|
+
gem.add_development_dependency 'guard-rspec'
|
25
|
+
gem.add_development_dependency 'shoulda-matchers'
|
26
|
+
gem.add_development_dependency 'rr'
|
27
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class MadokaModel
|
4
|
+
include Madoka::Model
|
5
|
+
attr_accessor :accessible_by_default, :protected_by_default
|
6
|
+
attr_accessor :accessible_by_admin
|
7
|
+
attr_accessible :accessible_by_default
|
8
|
+
attr_protected :protected_by_default
|
9
|
+
attr_accessible :accessible_by_admin, as: :admin
|
10
|
+
|
11
|
+
attr_accessor :flow
|
12
|
+
|
13
|
+
before_save :run_before_save
|
14
|
+
after_save :run_after_save
|
15
|
+
|
16
|
+
def run_before_save
|
17
|
+
@flow = [:before_save]
|
18
|
+
end
|
19
|
+
|
20
|
+
def run_after_save
|
21
|
+
@flow << :after_save
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe MadokaModel do
|
26
|
+
it_behaves_like 'ActiveModel'
|
27
|
+
it { should allow_mass_assignment_of(:accessible_by_default) }
|
28
|
+
it { should_not allow_mass_assignment_of(:protected_by_default) }
|
29
|
+
it { should_not allow_mass_assignment_of(:accessible_by_admin) }
|
30
|
+
it { should_not allow_mass_assignment_of(:accessible_by_default).as(:admin) }
|
31
|
+
it { should_not allow_mass_assignment_of(:protected_by_default).as(:admin) }
|
32
|
+
it { should allow_mass_assignment_of(:accessible_by_admin).as(:admin) }
|
33
|
+
|
34
|
+
describe '#save' do
|
35
|
+
context 'when valid' do
|
36
|
+
before { stub(subject).valid? { true } }
|
37
|
+
it { subject.should be_true }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when invalid' do
|
41
|
+
before { stub(subject).valid? { false } }
|
42
|
+
it { subject.save.should be_false }
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should run callback' do
|
46
|
+
subject.save
|
47
|
+
subject.flow.should == [:before_save, :after_save]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#save!' do
|
52
|
+
context 'when valid' do
|
53
|
+
before { stub(subject).valid? { true } }
|
54
|
+
it { lambda { subject.save! }.should_not raise_error }
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when invalid' do
|
58
|
+
before { stub(subject).valid? { false } }
|
59
|
+
it { lambda { subject.save! }.should raise_error(Madoka::ModelInvalid) }
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should run callback' do
|
63
|
+
subject.save!
|
64
|
+
subject.flow.should == [:before_save, :after_save]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#initialize' do
|
69
|
+
context 'when no args' do
|
70
|
+
it 'should not do assignment' do
|
71
|
+
any_instance_of(MadokaModel) do |model|
|
72
|
+
dont_allow(model).assign_attributes
|
73
|
+
end
|
74
|
+
MadokaModel.new
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when with params' do
|
79
|
+
it 'should assign attributes' do
|
80
|
+
attrs = {accessible_by_default: 'hello'}
|
81
|
+
any_instance_of(MadokaModel) do |model|
|
82
|
+
stub(model).assign_attributes(attrs, {})
|
83
|
+
end
|
84
|
+
MadokaModel.new(attrs)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require 'active_model'
|
8
|
+
require 'shoulda/matchers/integrations/rspec'
|
9
|
+
require 'madoka'
|
10
|
+
|
11
|
+
Dir['./spec/support/**/*.rb'].each {|f| require f}
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.mock_with :rr
|
15
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
16
|
+
config.run_all_when_everything_filtered = true
|
17
|
+
config.filter_run :focus
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# https://gist.github.com/639089
|
2
|
+
|
3
|
+
shared_examples_for 'ActiveModel' do
|
4
|
+
require 'test/unit/assertions'
|
5
|
+
require 'active_model/lint'
|
6
|
+
include Test::Unit::Assertions
|
7
|
+
include ActiveModel::Lint::Tests
|
8
|
+
|
9
|
+
# to_s is to support ruby-1.9
|
10
|
+
ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
|
11
|
+
example m.gsub('_',' ') do
|
12
|
+
send m
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def model
|
17
|
+
subject
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: madoka
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ayaya
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '2.5'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '2.5'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: guard-rspec
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: shoulda-matchers
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: rr
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
description: A model based on ActiveModel that provides mass assignment with role
|
143
|
+
support.
|
144
|
+
email:
|
145
|
+
- ayaya@ayaya.tw
|
146
|
+
executables: []
|
147
|
+
extensions: []
|
148
|
+
extra_rdoc_files: []
|
149
|
+
files:
|
150
|
+
- .gitignore
|
151
|
+
- .rspec
|
152
|
+
- Gemfile
|
153
|
+
- Guardfile
|
154
|
+
- LICENSE
|
155
|
+
- README.md
|
156
|
+
- Rakefile
|
157
|
+
- lib/active_model/model.rb
|
158
|
+
- lib/madoka.rb
|
159
|
+
- lib/madoka/model.rb
|
160
|
+
- lib/madoka/model_invalid.rb
|
161
|
+
- lib/madoka/version.rb
|
162
|
+
- madoka.gemspec
|
163
|
+
- spec/madoka/model_spec.rb
|
164
|
+
- spec/spec_helper.rb
|
165
|
+
- spec/support/active_model.rb
|
166
|
+
homepage: https://github.com/ayamomiji/madoka
|
167
|
+
licenses: []
|
168
|
+
post_install_message:
|
169
|
+
rdoc_options: []
|
170
|
+
require_paths:
|
171
|
+
- lib
|
172
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
+
none: false
|
174
|
+
requirements:
|
175
|
+
- - ! '>='
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
segments:
|
179
|
+
- 0
|
180
|
+
hash: -2715716810640895083
|
181
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
|
+
none: false
|
183
|
+
requirements:
|
184
|
+
- - ! '>='
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
segments:
|
188
|
+
- 0
|
189
|
+
hash: -2715716810640895083
|
190
|
+
requirements: []
|
191
|
+
rubyforge_project:
|
192
|
+
rubygems_version: 1.8.23
|
193
|
+
signing_key:
|
194
|
+
specification_version: 3
|
195
|
+
summary: A model based on ActiveModel that provides mass assignment with role support.
|
196
|
+
test_files:
|
197
|
+
- spec/madoka/model_spec.rb
|
198
|
+
- spec/spec_helper.rb
|
199
|
+
- spec/support/active_model.rb
|