model_loader 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/model_loader/version.rb +3 -0
- data/lib/model_loader.rb +63 -0
- data/model_loader.gemspec +21 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/model_loader.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Abstracts a common (at least for me) pattern in Rails applications. When
|
2
|
+
# creating controllers that interact with models it is common to have methods
|
3
|
+
# called find_<model_name> for your models. This module defines those find_*
|
4
|
+
# methods for you.
|
5
|
+
#
|
6
|
+
# When executing the find_* methods, if a matching record can not be found, the
|
7
|
+
# user is redirected to the 404 page.
|
8
|
+
#
|
9
|
+
# The module is activated by calling the +can_find_models+ method:
|
10
|
+
#
|
11
|
+
# class ApplicationController < ActionController::Base
|
12
|
+
# can_find_models
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# Valid options are :only and :except:
|
16
|
+
#
|
17
|
+
# class ApplicationController < ActionController
|
18
|
+
# can_find_models :only => 'Person'
|
19
|
+
# end
|
20
|
+
module ModelLoader
|
21
|
+
extend ActiveSupport::Concern
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
# Defines methods named find_<model_name> for each model in the application. The actual classes
|
25
|
+
# the methods are generated for can be controlled using the only or except options.
|
26
|
+
def can_find_models(options = {})
|
27
|
+
options.symbolize_keys!
|
28
|
+
|
29
|
+
raise ArgumentError, "The :only option to can_find_models must be an array." if options[:only] && !options[:only].is_a?(Array)
|
30
|
+
raise ArgumentError, "The :except option to can_find_models must be an array." if options[:except] && !options[:except].is_a?(Array)
|
31
|
+
|
32
|
+
if options[:only]
|
33
|
+
models = options[:only].map &:to_s
|
34
|
+
else
|
35
|
+
models = Dir["#{::Rails.root.to_s}/app/models/*"].map do |file|
|
36
|
+
File.basename(file, ".*").classify
|
37
|
+
end
|
38
|
+
|
39
|
+
models -= options[:except].map &:to_s if options[:except]
|
40
|
+
end
|
41
|
+
|
42
|
+
models.each do |model|
|
43
|
+
name = model.underscore
|
44
|
+
self.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
45
|
+
def find_#{name}
|
46
|
+
if params[:#{name}_id]
|
47
|
+
@#{name} = #{name.capitalize}.find(params[:#{name}_id])
|
48
|
+
else
|
49
|
+
@#{name} = #{name.capitalize}.find(params[:id])
|
50
|
+
end
|
51
|
+
rescue
|
52
|
+
resource_not_found
|
53
|
+
end
|
54
|
+
METHOD
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Renders the application's 404 page.
|
59
|
+
def resource_not_found
|
60
|
+
render :file => "#{::Rails.root.to_s}/public/404.html", :layout => false, :status => 404
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "model_loader/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "model_loader"
|
7
|
+
s.version = ModelLoader::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Adam Vaughan"]
|
10
|
+
s.email = ["adamjvaughan@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/adamvaughan/model_loader"
|
12
|
+
s.summary = %q{Automatically generate find method for models.}
|
13
|
+
s.description = %q{Automatically generates find_* methods for the models in your application.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "model_loader"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model_loader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Adam Vaughan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-17 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Automatically generates find_* methods for the models in your application.
|
23
|
+
email:
|
24
|
+
- adamjvaughan@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/model_loader.rb
|
36
|
+
- lib/model_loader/version.rb
|
37
|
+
- model_loader.gemspec
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: https://github.com/adamvaughan/model_loader
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: model_loader
|
68
|
+
rubygems_version: 1.4.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Automatically generate find method for models.
|
72
|
+
test_files: []
|
73
|
+
|