alfred 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@alfred --create
data/.travis.yml ADDED
@@ -0,0 +1 @@
1
+ rvm: 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in alfred.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/alfred.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "alfred/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "alfred"
7
+ s.version = Alfred::VERSION
8
+ s.authors = ["Daniel Spangenberg"]
9
+ s.email = ["daniel.spangenberg@parcydo.com"]
10
+ s.homepage = "http://rubygems.org/gems/alfred"
11
+ s.summary = %q{Alfred is the unobtrusive butler who takes care of the uninvited guests.}
12
+ s.description = %q{Alfred provides better attr_accessor handling on your application.}
13
+
14
+ s.rubyforge_project = "alfred"
15
+
16
+ s.add_dependency "rails", "~> 3.1.0.rc"
17
+ s.add_development_dependency "activerecord"
18
+ s.add_development_dependency "rr"
19
+ s.add_development_dependency "rspec"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
data/lib/alfred.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rails'
2
+
3
+ module Alfred
4
+ autoload :Models, 'alfred/models'
5
+
6
+ # Automaticly add password_confirmation if password exists.
7
+ mattr_accessor :auto_password_confirmation
8
+ @@auto_password_confirmation = true
9
+
10
+ # Default way to setup Alfred. Run rails generate alfred:install to create
11
+ # a fresh initializer with all configuration values.
12
+ def self.setup
13
+ yield self
14
+ end
15
+ end
16
+
17
+ require 'alfred/engine'
18
+ require 'alfred/orm/active_record' if defined?(ActiveRecord)
@@ -0,0 +1,4 @@
1
+ module Alfred
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Alfred
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Creates a Alfred initializer."
7
+
8
+ def copy_initializer
9
+ template "alfred.rb", "config/initializers/alfred.rb"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ # Use this hook to configure Alfred.
2
+ Alfred.setup do |config|
3
+ # Configure if password_confirmation should
4
+ # automaticly added if password exists.
5
+ # config.auto_password_confirmation = true
6
+ end
@@ -0,0 +1,53 @@
1
+ module Alfred
2
+ module Models
3
+ def alfred_accessible(*args)
4
+ alfred(:accessible, *args)
5
+ end
6
+
7
+ def alfred_protected(*args)
8
+ alfred(:protected, *args)
9
+ end
10
+
11
+ private
12
+
13
+ def alfred(method, *args)
14
+ options = args.extract_options!
15
+
16
+ if options[:as]
17
+ if method == :accessible
18
+ args = args + accessible_attributes(options[:inherit] || :default).to_a
19
+ else
20
+ args = args + protected_attributes(options[:inherit] || :default).to_a
21
+ end
22
+ args = args + [{ as: options[:as] }]
23
+ end
24
+
25
+ if args.include?(:password) && Alfred.auto_password_confirmation
26
+ args = args + [:password_confirmation]
27
+ end
28
+
29
+ if options[:on] && [:create, :update].include?(options[:on])
30
+ if options[:on] == :create
31
+ before = :before_create
32
+ else
33
+ before = :before_update
34
+ end
35
+ if method == :accessible
36
+ self.class.send(:define_method, before) do
37
+ attr_accessible(*args)
38
+ end
39
+ else
40
+ self.class.send(:define_method, before) do
41
+ attr_protected(*args)
42
+ end
43
+ end
44
+ else
45
+ if method == :accessible
46
+ attr_accessible(*args)
47
+ else
48
+ attr_protected(*args)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.extend Alfred::Models
@@ -0,0 +1,3 @@
1
+ module Alfred
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,209 @@
1
+ require 'spec_helper'
2
+
3
+ class AlfredModel
4
+ include ActiveModel::MassAssignmentSecurity
5
+ extend Alfred::Models
6
+ attr_accessor :a, :b, :c, :d, :e, :f
7
+ end
8
+
9
+ describe Alfred::Models do
10
+
11
+ describe "accessible" do
12
+
13
+ describe "create attr_accessible" do
14
+ it "add's attr_accessible with default role" do
15
+ class AAModel < AlfredModel
16
+ alfred_accessible :a, :b
17
+ end
18
+ AAModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
19
+ end
20
+
21
+ it "appends attr_accessible to default role" do
22
+ class ABModel < AlfredModel
23
+ alfred_accessible :a, :b
24
+ alfred_accessible :c, :d
25
+ end
26
+ ABModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
27
+ end
28
+
29
+ it "add's attr_accessible with custom role" do
30
+ class ACModel < AlfredModel
31
+ alfred_accessible :a, :b, as: :custom
32
+ end
33
+ ACModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
34
+ end
35
+
36
+ it "appends attr_accessible to custom role" do
37
+ class ADModel < AlfredModel
38
+ alfred_accessible :a, :b, as: :custom
39
+ alfred_accessible :c, :d, as: :custom
40
+ end
41
+ ADModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
42
+ end
43
+
44
+ it "add's attr_accessible to event" do
45
+ class AEModel < AlfredModel
46
+ alfred_accessible :a, :b, on: :create
47
+ end
48
+ AEModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
49
+ AEModel.before_create
50
+ AEModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
51
+ end
52
+
53
+ it "appends attr_accessible with custom role to event" do
54
+ class AFModel < AlfredModel
55
+ alfred_accessible :a, :b, as: :custom, on: :create
56
+ end
57
+ AFModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
58
+ AFModel.before_create
59
+ AFModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
60
+ AFModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
61
+ end
62
+ end
63
+
64
+ describe "inheritance" do
65
+ it "inherits from default role" do
66
+ class AGModel < AlfredModel
67
+ alfred_accessible :a, :b
68
+ alfred_accessible :c, :d, as: :custom
69
+ alfred_accessible :e, :f, as: :custom2
70
+ end
71
+ AGModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
72
+ AGModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
73
+ AGModel.accessible_attributes(:custom2).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :e, :f].map(&:to_s)))
74
+ end
75
+
76
+ it "inherits from custom role" do
77
+ class AHModel < AlfredModel
78
+ alfred_accessible :a, :b
79
+ alfred_accessible :c, :d, as: :custom
80
+ alfred_accessible :e, :f, as: :custom2, inherit: :custom
81
+ end
82
+ AHModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
83
+ AHModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
84
+ AHModel.accessible_attributes(:custom2).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d, :e, :f].map(&:to_s)))
85
+ end
86
+ end
87
+
88
+ describe "auto password confirmation" do
89
+ it "add's automatically password_confirmation if enabled" do
90
+ Alfred.auto_password_confirmation = true
91
+ class AIModel < AlfredModel
92
+ attr_accessor :password, :password_confirmation
93
+ alfred_accessible :password
94
+ end
95
+ AIModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:password, :password_confirmation].map(&:to_s)))
96
+ end
97
+
98
+ it "not add's automatically password_confirmation if disabled" do
99
+ Alfred.auto_password_confirmation = false
100
+ class AJModel < AlfredModel
101
+ attr_accessor :password, :password_confirmation
102
+ alfred_accessible :password
103
+ end
104
+ AJModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:password].map(&:to_s)))
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+ describe "protected" do
111
+
112
+ describe "create attr_protected" do
113
+ it "add's attr_protected with default role" do
114
+ class PAModel < AlfredModel
115
+ alfred_protected :a, :b
116
+ end
117
+ PAModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
118
+ end
119
+
120
+ it "appends attr_protected to default role" do
121
+ class PBModel < AlfredModel
122
+ alfred_protected :a, :b
123
+ alfred_protected :c, :d
124
+ end
125
+ PBModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :c, :d].map(&:to_s)))
126
+ end
127
+
128
+ it "add's attr_protected with custom role" do
129
+ class PCModel < AlfredModel
130
+ alfred_protected :a, :b, as: :custom
131
+ end
132
+ PCModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
133
+ end
134
+
135
+ it "appends attr_protected to custom role" do
136
+ class PDModel < AlfredModel
137
+ alfred_protected :a, :b, as: :custom
138
+ alfred_protected :c, :d, as: :custom
139
+ end
140
+ PDModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :c, :d].map(&:to_s)))
141
+ end
142
+
143
+ it "add's attr_protected to event" do
144
+ class PEModel < AlfredModel
145
+ alfred_protected :a, :b, on: :create
146
+ end
147
+ PEModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
148
+ PEModel.before_create
149
+ PEModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
150
+ end
151
+
152
+ it "appends attr_protected with custom role to event" do
153
+ class PFModel < AlfredModel
154
+ alfred_protected :a, :b, as: :custom, on: :create
155
+ end
156
+ PFModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
157
+ PFModel.before_create
158
+ PFModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
159
+ PFModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
160
+ end
161
+ end
162
+
163
+ describe "inheritance" do
164
+ it "inherits from default role" do
165
+ class PGModel < AlfredModel
166
+ alfred_protected :a, :b
167
+ alfred_protected :c, :d, as: :custom
168
+ alfred_protected :e, :f, as: :custom2
169
+ end
170
+ PGModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
171
+ PGModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :c, :d].map(&:to_s)))
172
+ PGModel.protected_attributes(:custom2).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :e, :f].map(&:to_s)))
173
+ end
174
+
175
+ it "inherits from custom role" do
176
+ class PHModel < AlfredModel
177
+ alfred_protected :a, :b
178
+ alfred_protected :c, :d, as: :custom
179
+ alfred_protected :e, :f, as: :custom2, inherit: :custom
180
+ end
181
+ PHModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
182
+ PHModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :c, :d].map(&:to_s)))
183
+ PHModel.protected_attributes(:custom2).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :c, :d, :e, :f].map(&:to_s)))
184
+ end
185
+ end
186
+
187
+ describe "auto password confirmation" do
188
+ it "add's automatically password_confirmation if enabled" do
189
+ Alfred.auto_password_confirmation = true
190
+ class PIModel < AlfredModel
191
+ attr_accessor :password, :password_confirmation
192
+ alfred_protected :password
193
+ end
194
+ PIModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:password, :password_confirmation].map(&:to_s)))
195
+ end
196
+
197
+ it "not add's automatically password_confirmation if disabled" do
198
+ Alfred.auto_password_confirmation = false
199
+ class PJModel < AlfredModel
200
+ attr_accessor :password, :password_confirmation
201
+ alfred_protected :password
202
+ end
203
+ PJModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:password].map(&:to_s)))
204
+ end
205
+ end
206
+
207
+ end
208
+
209
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require 'alfred'
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :rr
6
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alfred
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Spangenberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70320733483060 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0.rc
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70320733483060
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70320733482640 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70320733482640
36
+ - !ruby/object:Gem::Dependency
37
+ name: rr
38
+ requirement: &70320733482180 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70320733482180
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70320733481760 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70320733481760
58
+ description: Alfred provides better attr_accessor handling on your application.
59
+ email:
60
+ - daniel.spangenberg@parcydo.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rspec
67
+ - .rvmrc
68
+ - .travis.yml
69
+ - Gemfile
70
+ - Rakefile
71
+ - alfred.gemspec
72
+ - lib/alfred.rb
73
+ - lib/alfred/engine.rb
74
+ - lib/alfred/generators/alfred/install_generator.rb
75
+ - lib/alfred/generators/templates/alfred.rb
76
+ - lib/alfred/models.rb
77
+ - lib/alfred/orm/active_record.rb
78
+ - lib/alfred/version.rb
79
+ - spec/models_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: http://rubygems.org/gems/alfred
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project: alfred
101
+ rubygems_version: 1.8.8
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Alfred is the unobtrusive butler who takes care of the uninvited guests.
105
+ test_files:
106
+ - spec/models_spec.rb
107
+ - spec/spec_helper.rb