def_resource 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,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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in def_resource.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Biehl
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,49 @@
1
+ # DefResource
2
+
3
+ A way to DRY up Rails Controllers that are essentially providing an HTTP interface for a REST Resource.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'def_resource'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ The simplest way to use this gem is to execute the `def_resource` method in a controller
18
+
19
+ ```
20
+ class UsersController < ApplicationController
21
+ def_resource
22
+ end
23
+ ```
24
+
25
+ This essentially creates generic index, new, show, create, edit, update, and destroy methods on your controller for the User model.
26
+ If that is all you need, then you're done.
27
+ Of course you can override any of those method with your own by simply defining them in your controller.
28
+
29
+
30
+ ### Accessing your Models
31
+
32
+ For member actions, you can access the model instance in the view in one of two ways:
33
+
34
+ - The generic `resource` method
35
+ - The name of the model (eg: `user`)
36
+
37
+
38
+ For collection actions (like index), the collection can be accessed in one of two ways:
39
+
40
+ - The generic `resources` method
41
+ - The plural name of the model (eg: `users`)
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
3
+
4
+ # Custom for Visual Studio
5
+ *.cs diff=csharp
6
+ *.sln merge=union
7
+ *.csproj merge=union
8
+ *.vbproj merge=union
9
+ *.fsproj merge=union
10
+ *.dbproj merge=union
11
+
12
+ # Standard to msysgit
13
+ *.doc diff=astextplain
14
+ *.DOC diff=astextplain
15
+ *.docx diff=astextplain
16
+ *.DOCX diff=astextplain
17
+ *.dot diff=astextplain
18
+ *.DOT diff=astextplain
19
+ *.pdf diff=astextplain
20
+ *.PDF diff=astextplain
21
+ *.rtf diff=astextplain
22
+ *.RTF diff=astextplain
@@ -0,0 +1,163 @@
1
+ #################
2
+ ## Eclipse
3
+ #################
4
+
5
+ *.pydevproject
6
+ .project
7
+ .metadata
8
+ bin/
9
+ tmp/
10
+ *.tmp
11
+ *.bak
12
+ *.swp
13
+ *~.nib
14
+ local.properties
15
+ .classpath
16
+ .settings/
17
+ .loadpath
18
+
19
+ # External tool builders
20
+ .externalToolBuilders/
21
+
22
+ # Locally stored "Eclipse launch configurations"
23
+ *.launch
24
+
25
+ # CDT-specific
26
+ .cproject
27
+
28
+ # PDT-specific
29
+ .buildpath
30
+
31
+
32
+ #################
33
+ ## Visual Studio
34
+ #################
35
+
36
+ ## Ignore Visual Studio temporary files, build results, and
37
+ ## files generated by popular Visual Studio add-ons.
38
+
39
+ # User-specific files
40
+ *.suo
41
+ *.user
42
+ *.sln.docstates
43
+
44
+ # Build results
45
+ [Dd]ebug/
46
+ [Rr]elease/
47
+ *_i.c
48
+ *_p.c
49
+ *.ilk
50
+ *.meta
51
+ *.obj
52
+ *.pch
53
+ *.pdb
54
+ *.pgc
55
+ *.pgd
56
+ *.rsp
57
+ *.sbr
58
+ *.tlb
59
+ *.tli
60
+ *.tlh
61
+ *.tmp
62
+ *.vspscc
63
+ .builds
64
+ *.dotCover
65
+
66
+ ## TODO: If you have NuGet Package Restore enabled, uncomment this
67
+ #packages/
68
+
69
+ # Visual C++ cache files
70
+ ipch/
71
+ *.aps
72
+ *.ncb
73
+ *.opensdf
74
+ *.sdf
75
+
76
+ # Visual Studio profiler
77
+ *.psess
78
+ *.vsp
79
+
80
+ # ReSharper is a .NET coding add-in
81
+ _ReSharper*
82
+
83
+ # Installshield output folder
84
+ [Ee]xpress
85
+
86
+ # DocProject is a documentation generator add-in
87
+ DocProject/buildhelp/
88
+ DocProject/Help/*.HxT
89
+ DocProject/Help/*.HxC
90
+ DocProject/Help/*.hhc
91
+ DocProject/Help/*.hhk
92
+ DocProject/Help/*.hhp
93
+ DocProject/Help/Html2
94
+ DocProject/Help/html
95
+
96
+ # Click-Once directory
97
+ publish
98
+
99
+ # Others
100
+ [Bb]in
101
+ [Oo]bj
102
+ sql
103
+ TestResults
104
+ *.Cache
105
+ ClientBin
106
+ stylecop.*
107
+ ~$*
108
+ *.dbmdl
109
+ Generated_Code #added for RIA/Silverlight projects
110
+
111
+ # Backup & report files from converting an old project file to a newer
112
+ # Visual Studio version. Backup files are not needed, because we have git ;-)
113
+ _UpgradeReport_Files/
114
+ Backup*/
115
+ UpgradeLog*.XML
116
+
117
+
118
+
119
+ ############
120
+ ## Windows
121
+ ############
122
+
123
+ # Windows image file caches
124
+ Thumbs.db
125
+
126
+ # Folder config file
127
+ Desktop.ini
128
+
129
+
130
+ #############
131
+ ## Python
132
+ #############
133
+
134
+ *.py[co]
135
+
136
+ # Packages
137
+ *.egg
138
+ *.egg-info
139
+ dist
140
+ build
141
+ eggs
142
+ parts
143
+ bin
144
+ var
145
+ sdist
146
+ develop-eggs
147
+ .installed.cfg
148
+
149
+ # Installer logs
150
+ pip-log.txt
151
+
152
+ # Unit test / coverage reports
153
+ .coverage
154
+ .tox
155
+
156
+ #Translations
157
+ *.mo
158
+
159
+ #Mr Developer
160
+ .mr.developer.cfg
161
+
162
+ # Mac crap
163
+ .DS_Store
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'def_resource/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "def_resource"
8
+ gem.version = DefResource::VERSION
9
+ gem.authors = ["David Biehl"]
10
+ gem.email = ["lazylodr@gmail.com"]
11
+ gem.summary = %q{Defines Rails Controllers as Resources to reduce boilerplate code}
12
+ gem.homepage = ""
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ end
@@ -0,0 +1,15 @@
1
+ module DefResource
2
+ class Config
3
+ attr_accessor :model, :resource
4
+
5
+ def initialize &block
6
+ instance_eval &block if block_given?
7
+ end
8
+
9
+ def resource= resource
10
+ super
11
+ @model = resource.class
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,63 @@
1
+ require "def_resource/interface"
2
+
3
+ module DefResource
4
+ module Controller
5
+ def self.included base
6
+ base.send(:helper_method, :resource)
7
+ base.send(:before_filter, :def_resource_before_filter)
8
+ end
9
+
10
+ def resource
11
+ @resource ||= def_resource.resource
12
+ end
13
+
14
+ def resources
15
+ @resources ||= def_resource.resources
16
+ end
17
+
18
+ def index
19
+ respond_with resources
20
+ end
21
+
22
+ def new
23
+ respond_with def_resource.model.new
24
+ end
25
+
26
+ def create
27
+ resource = def_resource.model.create(params[def_resource.param_key])
28
+ respond_with resource
29
+ end
30
+
31
+ def show
32
+ respond_with resource
33
+ end
34
+
35
+ def edit
36
+ respond_with resource
37
+ end
38
+
39
+ def update
40
+ resource.update_attributes(params[def_resource.param_key])
41
+ respond_with resource
42
+ end
43
+
44
+ def destroy
45
+ resource.destroy
46
+ respond_with resource
47
+ end
48
+
49
+ private
50
+
51
+ def def_resource
52
+ @def_resource ||= DefResource::Interface.new self
53
+ end
54
+
55
+ def def_resource_before_filter
56
+ self.class.send(:alias_method, def_resource.param_key, :resource)
57
+ self.class.send(:helper_method, def_resource.param_key)
58
+
59
+ self.class.send(:alias_method, def_resource.param_key.pluralize, :resources)
60
+ self.class.send(:helper_method, def_resource.param_key.pluralize)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,24 @@
1
+ require "def_resource/controller"
2
+ require "def_resource/config"
3
+
4
+ module DefResource
5
+ module ControllerExtension
6
+ def self.included base
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ attr_reader :def_resource_config
12
+
13
+ def def_resource &block
14
+ include DefResource::Controller
15
+
16
+ if block_given?
17
+ @def_resource_config = DefResource::Config.new &block
18
+ else
19
+ @def_resource_config = DefResource::Config.new
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ require "def_resource/controller_extension"
2
+
3
+ module DefResource
4
+ class Engine < Rails::Engine
5
+ config.after_initialize do
6
+ ActionController::Base.class_eval do
7
+ include DefResource::ControllerExtension
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,47 @@
1
+ module DefResource
2
+ class Interface
3
+ attr_reader :controller
4
+
5
+ def initialize controller
6
+ @controller = controller
7
+ end
8
+
9
+ def model
10
+ config.model || default_model
11
+ end
12
+
13
+ def config
14
+ controller.class.def_resource_config
15
+ end
16
+
17
+ def resource
18
+ config.resource || find_resource
19
+ end
20
+
21
+ def resources
22
+ find_resources
23
+ end
24
+
25
+ def param_key
26
+ model.model_name.param_key
27
+ end
28
+
29
+ private
30
+
31
+ def params
32
+ controller.params
33
+ end
34
+
35
+ def find_resource
36
+ model.find(params[:id])
37
+ end
38
+
39
+ def find_resources
40
+ model.limit(10)
41
+ end
42
+
43
+ def default_model
44
+ controller.class.to_s.chomp("Controller").singularize.constantize
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module DefResource
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ module DefResource
2
+ # Your code goes here...
3
+ end
4
+
5
+ require "def_resource/version"
6
+ require "def_resource/engine"
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: def_resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Biehl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - lazylodr@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - def_resource.gemspec
27
+ - def_resource/.gitattributes
28
+ - def_resource/.gitignore
29
+ - lib/def_resource.rb
30
+ - lib/def_resource/config.rb
31
+ - lib/def_resource/controller.rb
32
+ - lib/def_resource/controller_extension.rb
33
+ - lib/def_resource/engine.rb
34
+ - lib/def_resource/interface.rb
35
+ - lib/def_resource/version.rb
36
+ homepage: ''
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.11
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Defines Rails Controllers as Resources to reduce boilerplate code
60
+ test_files: []