exposure 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +41 -0
- data/README.mdown +39 -0
- data/Rakefile +26 -0
- data/exposure.gemspec +31 -0
- data/lib/exposure/common.rb +127 -0
- data/lib/exposure/configuration.rb +63 -0
- data/lib/exposure/patterns/resource.rb +226 -0
- data/lib/exposure/patterns/resources.rb +323 -0
- data/lib/exposure.rb +14 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/custom_matchers.rb +41 -0
- data/spec/exposure_spec.rb +4 -0
- data/spec/factories/pirate_factory.rb +8 -0
- data/spec/factories/ship_factory.rb +3 -0
- data/spec/finders/finder_spec.rb +53 -0
- data/spec/finders/nested_resources_finder_spec.rb +29 -0
- data/spec/fixtures/pirates/edit.erb +0 -0
- data/spec/fixtures/pirates/index.erb +0 -0
- data/spec/fixtures/pirates/new.erb +0 -0
- data/spec/fixtures/pirates/show.erb +0 -0
- data/spec/fixtures/ships/edit.erb +0 -0
- data/spec/fixtures/ships/index.erb +0 -0
- data/spec/fixtures/ships/new.erb +0 -0
- data/spec/fixtures/ships/show.erb +0 -0
- data/spec/flashers/flash_with_block_spec.rb +32 -0
- data/spec/flashers/flash_with_method_spec.rb +86 -0
- data/spec/flashers/flash_with_proc_spec.rb +83 -0
- data/spec/flashers/flash_with_string_spec.rb +82 -0
- data/spec/resource_spec.rb +192 -0
- data/spec/resources_spec.rb +204 -0
- data/spec/responders/respond_to_mime_types_spec.rb +44 -0
- data/spec/responders/respond_with_block_spec.rb +84 -0
- data/spec/responders/respond_with_method_spec.rb +84 -0
- data/spec/responders/respond_with_proc_spec.rb +79 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +71 -0
- data/spec/spec_rails_helper.rb +22 -0
- data/tasks/rspec.rake +21 -0
- data/tasks/shoulda.rake +15 -0
- metadata +106 -0
data/Manifest.txt
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
Manifest.txt
|
2
|
+
README.mdown
|
3
|
+
Rakefile
|
4
|
+
exposure.gemspec
|
5
|
+
lib/exposure.rb
|
6
|
+
lib/exposure/common.rb
|
7
|
+
lib/exposure/configuration.rb
|
8
|
+
lib/exposure/patterns/resource.rb
|
9
|
+
lib/exposure/patterns/resources.rb
|
10
|
+
script/console
|
11
|
+
script/destroy
|
12
|
+
script/generate
|
13
|
+
spec/custom_matchers.rb
|
14
|
+
spec/exposure_spec.rb
|
15
|
+
spec/factories/pirate_factory.rb
|
16
|
+
spec/factories/ship_factory.rb
|
17
|
+
spec/finders/finder_spec.rb
|
18
|
+
spec/finders/nested_resources_finder_spec.rb
|
19
|
+
spec/fixtures/pirates/edit.erb
|
20
|
+
spec/fixtures/pirates/index.erb
|
21
|
+
spec/fixtures/pirates/new.erb
|
22
|
+
spec/fixtures/pirates/show.erb
|
23
|
+
spec/fixtures/ships/edit.erb
|
24
|
+
spec/fixtures/ships/index.erb
|
25
|
+
spec/fixtures/ships/new.erb
|
26
|
+
spec/fixtures/ships/show.erb
|
27
|
+
spec/flashers/flash_with_block_spec.rb
|
28
|
+
spec/flashers/flash_with_method_spec.rb
|
29
|
+
spec/flashers/flash_with_proc_spec.rb
|
30
|
+
spec/flashers/flash_with_string_spec.rb
|
31
|
+
spec/resource_spec.rb
|
32
|
+
spec/resources_spec.rb
|
33
|
+
spec/responders/respond_to_mime_types_spec.rb
|
34
|
+
spec/responders/respond_with_block_spec.rb
|
35
|
+
spec/responders/respond_with_method_spec.rb
|
36
|
+
spec/responders/respond_with_proc_spec.rb
|
37
|
+
spec/spec.opts
|
38
|
+
spec/spec_helper.rb
|
39
|
+
spec/spec_rails_helper.rb
|
40
|
+
tasks/rspec.rake
|
41
|
+
tasks/shoulda.rake
|
data/README.mdown
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
exposure
|
2
|
+
======
|
3
|
+
|
4
|
+
REQUIREMENTS:
|
5
|
+
======
|
6
|
+
|
7
|
+
|
8
|
+
INSTALL:
|
9
|
+
=======
|
10
|
+
|
11
|
+
gem sources -a http://gems.github.com (you only have to do this once)
|
12
|
+
sudo gem install trek-exposure
|
13
|
+
|
14
|
+
|
15
|
+
LICENSE:
|
16
|
+
=========
|
17
|
+
|
18
|
+
(The MIT License)
|
19
|
+
|
20
|
+
Copyright (c) 2009 Trek Glowacki
|
21
|
+
|
22
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
23
|
+
a copy of this software and associated documentation files (the
|
24
|
+
'Software'), to deal in the Software without restriction, including
|
25
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
26
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
27
|
+
permit persons to whom the Software is furnished to do so, subject to
|
28
|
+
the following conditions:
|
29
|
+
|
30
|
+
The above copyright notice and this permission notice shall be
|
31
|
+
included in all copies or substantial portions of the Software.
|
32
|
+
|
33
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
34
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
35
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
36
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
37
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
38
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
39
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/exposure'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'exposure' do
|
14
|
+
self.developer 'Trek Glowacki', 'trek.glowacki@gmail.com'
|
15
|
+
# self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
16
|
+
# self.rubyforge_name = self.name # TODO this is default value
|
17
|
+
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'newgem/tasks'
|
22
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
|
+
|
24
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
25
|
+
# remove_task :default
|
26
|
+
# task :default => [:spec, :features]
|
data/exposure.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{exposure}
|
5
|
+
s.version = "0.0.4"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Trek Glowacki"]
|
9
|
+
s.date = %q{2009-10-07}
|
10
|
+
s.description = %q{Expose your data}
|
11
|
+
s.email = ["trek.glowacki@gmail.com"]
|
12
|
+
s.extra_rdoc_files = ["Manifest.txt"]
|
13
|
+
s.files = ["Manifest.txt", "README.mdown", "Rakefile", "exposure.gemspec", "lib/exposure.rb", "lib/exposure/common.rb", "lib/exposure/configuration.rb", "lib/exposure/patterns/resource.rb", "lib/exposure/patterns/resources.rb", "script/console", "script/destroy", "script/generate", "spec/custom_matchers.rb", "spec/exposure_spec.rb", "spec/factories/pirate_factory.rb", "spec/factories/ship_factory.rb", "spec/finders/finder_spec.rb", "spec/finders/nested_resources_finder_spec.rb", "spec/fixtures/pirates/edit.erb", "spec/fixtures/pirates/index.erb", "spec/fixtures/pirates/new.erb", "spec/fixtures/pirates/show.erb", "spec/fixtures/ships/edit.erb", "spec/fixtures/ships/index.erb", "spec/fixtures/ships/new.erb", "spec/fixtures/ships/show.erb", "spec/flashers/flash_with_block_spec.rb", "spec/flashers/flash_with_method_spec.rb", "spec/flashers/flash_with_proc_spec.rb", "spec/flashers/flash_with_string_spec.rb", "spec/resource_spec.rb", "spec/resources_spec.rb", "spec/responders/respond_to_mime_types_spec.rb", "spec/responders/respond_with_block_spec.rb", "spec/responders/respond_with_method_spec.rb", "spec/responders/respond_with_proc_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/spec_rails_helper.rb", "tasks/rspec.rake", "tasks/shoulda.rake"]
|
14
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.rubygems_version = %q{1.3.4}
|
17
|
+
s.summary = %q{Expose your data}
|
18
|
+
|
19
|
+
if s.respond_to? :specification_version then
|
20
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
21
|
+
s.specification_version = 3
|
22
|
+
|
23
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
24
|
+
s.add_development_dependency(%q<hoe>, [">= 2.3.3"])
|
25
|
+
else
|
26
|
+
s.add_dependency(%q<hoe>, [">= 2.3.3"])
|
27
|
+
end
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<hoe>, [">= 2.3.3"])
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
module Exposure
|
2
|
+
module Common
|
3
|
+
class <<
|
4
|
+
attr_accessor :resource_name, :resources_name,
|
5
|
+
:resource_chain, :resources_chain,
|
6
|
+
:collection_nesting, :member_nesting,
|
7
|
+
:parent_model
|
8
|
+
end
|
9
|
+
|
10
|
+
# response_for :create, :on => :success, :is => { proc }
|
11
|
+
# response_for :show, :formats => [:html] do
|
12
|
+
# @resource.activated ? render('show') : render('next_steps')
|
13
|
+
# end
|
14
|
+
# response_for :new, :is => :new_foo_built
|
15
|
+
#
|
16
|
+
# valid action names are
|
17
|
+
# :index :show :new :create :edit :update :destroy
|
18
|
+
# valid options are
|
19
|
+
# :on (optional)
|
20
|
+
# :success, :failure, :any
|
21
|
+
# default is :any
|
22
|
+
# :is (option if block given)
|
23
|
+
# can be a Proc or method name as symbol.
|
24
|
+
# :formats
|
25
|
+
# array of formats as symbols.
|
26
|
+
# defaults to [:html]
|
27
|
+
def response_for(action_name, options = {}, &block)
|
28
|
+
options[:is] ||= block
|
29
|
+
options[:formats] ||= [:html]
|
30
|
+
case options[:on]
|
31
|
+
when NilClass, :any
|
32
|
+
self.const_get(:Responses)["#{action_name}.success.html"] = options[:is]
|
33
|
+
self.const_get(:Responses)["#{action_name}.failure.html"] = options[:is]
|
34
|
+
when :success
|
35
|
+
self.const_get(:Responses)["#{action_name}.success.html"] = options[:is]
|
36
|
+
when :failure
|
37
|
+
self.const_get(:Responses)["#{action_name}.failure.html"] = options[:is]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# find :person, :with => Proc.new { Person.find_by_permalink(params[:permalink]) }
|
42
|
+
# find :people, :with => Proc.new { Person.send(params[:scope]) }
|
43
|
+
# find :dogs, :with => :dogs_adopted_after_date
|
44
|
+
# find :dogs do
|
45
|
+
# Dog.all
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# valid options are
|
49
|
+
# :with
|
50
|
+
# :only (unimplemented)
|
51
|
+
# :except (unimplemented)
|
52
|
+
def find(name, options = {}, &block)
|
53
|
+
options[:with] ||= block
|
54
|
+
self.const_get(:Finders)[name] = options[:with]
|
55
|
+
end
|
56
|
+
|
57
|
+
# configure flash messages
|
58
|
+
# valid action names are
|
59
|
+
# :index :show :new :create :edit :update :destroy
|
60
|
+
# valid options are
|
61
|
+
# :on (optional)
|
62
|
+
# :success, :failure, :any
|
63
|
+
# default is :any
|
64
|
+
# :is (optional if block given)
|
65
|
+
# can be a Proc or method name as symbol.
|
66
|
+
def flash_for(action_name, options = {}, &block)
|
67
|
+
options[:is] ||= block
|
68
|
+
|
69
|
+
case options[:on]
|
70
|
+
when NilClass, :any
|
71
|
+
self.const_get(:FlashMessages)[true][action_name] = options[:is]
|
72
|
+
self.const_get(:FlashMessages)[false][action_name] = options[:is]
|
73
|
+
when :success
|
74
|
+
self.const_get(:FlashMessages)[true][action_name] = options[:is]
|
75
|
+
when :failure
|
76
|
+
self.const_get(:FlashMessages)[false][action_name] = options[:is]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# access point for creating and configuring before_ callbacks.
|
81
|
+
def before(trigger, action, options = {})
|
82
|
+
build_callback('before', trigger, action, options)
|
83
|
+
end
|
84
|
+
|
85
|
+
# access point for creating and configuring before_ callbacks.
|
86
|
+
def after(trigger, action, options = {})
|
87
|
+
build_callback('after', trigger, action, options)
|
88
|
+
end
|
89
|
+
|
90
|
+
# builds default finders
|
91
|
+
def build_default_finders(member, nesting) #:nodoc:
|
92
|
+
finders = self::const_set(:DefaultFinders, {
|
93
|
+
self.resource_name.intern => Proc.new { [:find, params[:id] ] },
|
94
|
+
self.resources_name.intern => Proc.new { [:all] }
|
95
|
+
})
|
96
|
+
|
97
|
+
nesting.each do |association_name|
|
98
|
+
finders[association_name.to_s.singularize.to_sym] = Proc.new { [:find, params[:"#{association_name.to_s.singularize}_id"]] }
|
99
|
+
finders[association_name] = Proc.new { [ :all ] }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# builds callbacks that adhere to the ActiveSupport::Callbacks interface
|
104
|
+
def build_callback(prefix, trigger, action, options) #:nodoc:
|
105
|
+
callback_name = "#{prefix}_#{trigger}"
|
106
|
+
|
107
|
+
if options[:on]
|
108
|
+
callback_name += "_on_#{options.delete(:on)}"
|
109
|
+
end
|
110
|
+
|
111
|
+
options[:if] ||= []
|
112
|
+
|
113
|
+
only_methods = options.delete(:only)
|
114
|
+
except_methods = options.delete(:except)
|
115
|
+
|
116
|
+
if only_methods
|
117
|
+
options[:if] << Proc.new {|c| only_methods.include?(c.action_name.intern) }
|
118
|
+
end
|
119
|
+
|
120
|
+
if except_methods
|
121
|
+
options[:if] << Proc.new {|c| !except_methods.include?(c.action_name.intern) }
|
122
|
+
end
|
123
|
+
|
124
|
+
self.send(callback_name, action, options)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Exposure
|
2
|
+
module Configuration
|
3
|
+
# options
|
4
|
+
# :nested => false or symbol or array of symbols
|
5
|
+
# defaults to false
|
6
|
+
# :only => array of REST methods names as symbols to only include
|
7
|
+
# defaults to [:show, :new, :create, :edit, :update, :destroy]
|
8
|
+
# :except => array of REST methods to exclude
|
9
|
+
# defaults to [ ]
|
10
|
+
# :formats => array of
|
11
|
+
# defaults to [ :html, :xml, :json ]
|
12
|
+
def expose_one(resource_name, options = {})
|
13
|
+
include ActiveSupport::Callbacks
|
14
|
+
extend Exposure::Common
|
15
|
+
self.resource_name = resource_name
|
16
|
+
self.member_nesting = [ [self.resource_name.to_sym] ]
|
17
|
+
|
18
|
+
extend Patterns::Resource
|
19
|
+
include Patterns::Resource::Actions
|
20
|
+
|
21
|
+
define_callbacks(*Patterns::Resource::Callbacks)
|
22
|
+
end
|
23
|
+
|
24
|
+
# options
|
25
|
+
# :nested => false or symbol or array of symbols
|
26
|
+
# defaults to false
|
27
|
+
# :only => array of REST methods names as symbols to only include
|
28
|
+
# defaults to [:index, :show, :new, :create, :edit, :update, :destroy]
|
29
|
+
# :except => array of REST methods to exclude
|
30
|
+
# defaults to [ ]
|
31
|
+
# :formats => array of
|
32
|
+
# defaults to [ :html, :xml, :json ]
|
33
|
+
#
|
34
|
+
def expose_many(name, options = {})
|
35
|
+
include ActiveSupport::Callbacks
|
36
|
+
extend Exposure::Common
|
37
|
+
|
38
|
+
self.resource_name = name.to_s.singularize
|
39
|
+
self.resources_name = name.to_s
|
40
|
+
|
41
|
+
if nesting = options[:nested]
|
42
|
+
|
43
|
+
self.parent_model = nesting.shift.to_s.singularize.capitalize.constantize
|
44
|
+
|
45
|
+
build_default_finders(self.resources_name, nesting)
|
46
|
+
|
47
|
+
nesting.collect! {|sym| [sym.to_s.singularize.to_sym, sym]}
|
48
|
+
self.member_nesting = nesting + [ [self.resource_name.to_sym] ]
|
49
|
+
self.collection_nesting = nesting + [ [self.resources_name.to_sym] ]
|
50
|
+
else
|
51
|
+
self.parent_model = self.resource_name.capitalize.constantize
|
52
|
+
build_default_finders(self.resource_name, [])
|
53
|
+
self.member_nesting = [ [self.resource_name.to_sym] ]
|
54
|
+
self.collection_nesting = [ [self.resources_name.to_sym] ]
|
55
|
+
end
|
56
|
+
|
57
|
+
extend Patterns::Resources
|
58
|
+
include Patterns::Resources::Actions
|
59
|
+
|
60
|
+
define_callbacks(*Patterns::Resources::Callbacks)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,226 @@
|
|
1
|
+
module Exposure
|
2
|
+
module Patterns
|
3
|
+
module Resource
|
4
|
+
def self.extended(base)
|
5
|
+
base::const_set(:DefaultResponses, DefaultResponses)
|
6
|
+
base::const_set(:DefaultFlashMessages, DefaultFlashMessages)
|
7
|
+
base::const_set(:DefaultFinders, {
|
8
|
+
base.resource_name => Proc.new {|parent,controller| parent.find(controller.params[:id])},
|
9
|
+
})
|
10
|
+
end
|
11
|
+
|
12
|
+
Callbacks = %w(
|
13
|
+
before_find after_find after_find_on_failure after_find_on_success
|
14
|
+
before_assign after_assign
|
15
|
+
before_save after_save after_save_on_failure after_save_on_success
|
16
|
+
before_create after_create_on_failure after_create_on_success
|
17
|
+
before_update after_update_on_failure after_update_on_success
|
18
|
+
before_destroy after_destroy_on_success
|
19
|
+
before_response before_response_on_success before_response_on_failure
|
20
|
+
)
|
21
|
+
|
22
|
+
DefaultFlashMessages = {
|
23
|
+
true => {
|
24
|
+
:create => Proc.new {|c| "#{c.send(:resource_name).capitalize} successfully created" },
|
25
|
+
:update => Proc.new {|c| "#{c.send(:resource_name).capitalize} successfully updated" },
|
26
|
+
:destroy => Proc.new {|c| "#{c.send(:resource_name).capitalize} successfully removed" }
|
27
|
+
},
|
28
|
+
false => {}
|
29
|
+
}
|
30
|
+
|
31
|
+
DefaultResponses = {
|
32
|
+
true => {
|
33
|
+
:show => Proc.new {|c| c.send(:render, 'show') },
|
34
|
+
:new => Proc.new {|c| c.send(:render, 'new') },
|
35
|
+
:create => Proc.new {|c| c.send(:redirect_to, {:action => 'show'}) },
|
36
|
+
:edit => Proc.new {|c| c.send(:render, 'edit') },
|
37
|
+
:update => Proc.new {|c| c.send(:redirect_to, {:action => 'show' }) },
|
38
|
+
:destroy => Proc.new {|c| c.send(:redirect_to, {:action => 'new'}) }
|
39
|
+
},
|
40
|
+
false => {
|
41
|
+
:create => Proc.new {|c| c.send(:render, 'new') },
|
42
|
+
:update => Proc.new {|c| c.send(:render, 'edit') }
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
module Actions
|
47
|
+
# no index on single resources
|
48
|
+
|
49
|
+
def show
|
50
|
+
run_callbacks(:before_find)
|
51
|
+
if find_record
|
52
|
+
run_callbacks(:after_find_on_success)
|
53
|
+
run_callbacks(:after_find)
|
54
|
+
run_callbacks(:before_response)
|
55
|
+
run_callbacks(:before_response_on_success)
|
56
|
+
response_for(:show, true)
|
57
|
+
else
|
58
|
+
run_callbacks(:after_find_on_failure)
|
59
|
+
run_callbacks(:after_find)
|
60
|
+
run_callbacks(:before_response)
|
61
|
+
run_callbacks(:before_response_on_failure)
|
62
|
+
response_for(:show, false)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def new
|
67
|
+
run_callbacks(:before_assign)
|
68
|
+
new_record
|
69
|
+
run_callbacks(:after_assign)
|
70
|
+
run_callbacks(:before_response)
|
71
|
+
run_callbacks(:before_response_on_success)
|
72
|
+
response_for(:new, true)
|
73
|
+
end
|
74
|
+
|
75
|
+
def create
|
76
|
+
run_callbacks(:before_assign)
|
77
|
+
new_record
|
78
|
+
run_callbacks(:after_assign)
|
79
|
+
|
80
|
+
run_callbacks(:before_create)
|
81
|
+
run_callbacks(:before_save)
|
82
|
+
|
83
|
+
if save_record
|
84
|
+
run_callbacks(:after_save_on_success)
|
85
|
+
run_callbacks(:after_create_on_success)
|
86
|
+
run_callbacks(:before_response)
|
87
|
+
run_callbacks(:before_response_on_success)
|
88
|
+
flash_for(:create, true)
|
89
|
+
response_for(:create, true)
|
90
|
+
else
|
91
|
+
run_callbacks(:after_save_on_failure)
|
92
|
+
run_callbacks(:after_create_on_failure)
|
93
|
+
run_callbacks(:before_response)
|
94
|
+
run_callbacks(:before_response_on_failure)
|
95
|
+
flash_for(:create, false)
|
96
|
+
response_for(:create, false)
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
def edit
|
102
|
+
run_callbacks(:before_find)
|
103
|
+
if find_record
|
104
|
+
run_callbacks(:after_find_on_success)
|
105
|
+
run_callbacks(:after_find)
|
106
|
+
run_callbacks(:before_response)
|
107
|
+
run_callbacks(:before_response_on_success)
|
108
|
+
response_for(:edit, true)
|
109
|
+
else
|
110
|
+
run_callbacks(:after_find_on_failure)
|
111
|
+
run_callbacks(:after_find)
|
112
|
+
run_callbacks(:before_response)
|
113
|
+
run_callbacks(:before_response_on_failure)
|
114
|
+
response_for(:edit, false)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def update
|
119
|
+
run_callbacks(:before_find)
|
120
|
+
if find_record
|
121
|
+
run_callbacks(:after_find_on_success)
|
122
|
+
run_callbacks(:after_find)
|
123
|
+
if update_record
|
124
|
+
run_callbacks(:after_save_on_success)
|
125
|
+
run_callbacks(:after_update_on_success)
|
126
|
+
run_callbacks(:before_response)
|
127
|
+
run_callbacks(:before_response_on_success)
|
128
|
+
flash_for(:update, true)
|
129
|
+
response_for(:update, true)
|
130
|
+
else
|
131
|
+
run_callbacks(:after_save_on_failure)
|
132
|
+
run_callbacks(:after_create_on_failure)
|
133
|
+
run_callbacks(:before_response)
|
134
|
+
run_callbacks(:before_response_on_failure)
|
135
|
+
flash_for(:update, false)
|
136
|
+
response_for(:update, false)
|
137
|
+
end
|
138
|
+
else
|
139
|
+
run_callbacks(:after_find_on_failure)
|
140
|
+
run_callbacks(:after_find)
|
141
|
+
run_callbacks(:before_response)
|
142
|
+
run_callbacks(:before_response_on_failure)
|
143
|
+
response_for(:edit, false)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def destroy
|
148
|
+
run_callbacks(:before_find)
|
149
|
+
if find_record
|
150
|
+
run_callbacks(:after_find_on_success)
|
151
|
+
run_callbacks(:after_find)
|
152
|
+
run_callbacks(:before_destroy)
|
153
|
+
|
154
|
+
delete_record
|
155
|
+
|
156
|
+
run_callbacks(:after_destroy_on_success)
|
157
|
+
run_callbacks(:before_response)
|
158
|
+
run_callbacks(:before_response_on_success)
|
159
|
+
flash_for(:destroy, true)
|
160
|
+
response_for(:destroy, true)
|
161
|
+
|
162
|
+
else
|
163
|
+
run_callbacks(:after_find_on_failure)
|
164
|
+
run_callbacks(:after_find)
|
165
|
+
response_for(:destroy, false)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
def custom_response_for(action_name, action_successful)
|
171
|
+
false
|
172
|
+
end
|
173
|
+
|
174
|
+
def default_response_for(action_name, action_successful)
|
175
|
+
self.class::DefaultResponses[action_successful][action_name].call(self)
|
176
|
+
end
|
177
|
+
|
178
|
+
def response_for(action_name, action_successful)
|
179
|
+
custom_response_for(action_name, action_successful) || default_response_for(action_name, action_successful)
|
180
|
+
end
|
181
|
+
|
182
|
+
def custom_flash_for(action_name, action_successful)
|
183
|
+
false
|
184
|
+
end
|
185
|
+
|
186
|
+
def default_flash_for(action_name, action_successful)
|
187
|
+
if message_proc = self.class::DefaultFlashMessages[action_successful][action_name]
|
188
|
+
flash[:message] = message_proc.call(self)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def flash_for(action_name, action_successful)
|
193
|
+
custom_flash_for(action_name, action_successful) || default_flash_for(action_name, action_successful)
|
194
|
+
end
|
195
|
+
|
196
|
+
def resource_name
|
197
|
+
self.class.resource_name
|
198
|
+
end
|
199
|
+
|
200
|
+
def save_record
|
201
|
+
@resource.save
|
202
|
+
end
|
203
|
+
|
204
|
+
def update_record
|
205
|
+
@resource.update_attributes(params[resource_name])
|
206
|
+
end
|
207
|
+
|
208
|
+
def new_record
|
209
|
+
@resource = instance_variable_set("@#{resource_name}", resource_name.camelize.constantize.new(params[resource_name]))
|
210
|
+
end
|
211
|
+
|
212
|
+
def find_record
|
213
|
+
@resource = instance_variable_set("@#{resource_name}", resource_name.camelize.constantize.find(params[:id]))
|
214
|
+
end
|
215
|
+
|
216
|
+
def find_records
|
217
|
+
@resources = instance_variable_set("@#{resource_name.pluralize}", resource_name.camelize.constantize.all)
|
218
|
+
end
|
219
|
+
|
220
|
+
def delete_record
|
221
|
+
@resource.destroy
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|