happy-resources 0.1.0.pre1

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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in happy-resources.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Hendrik Mans
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,4 @@
1
+ # Happy::Resources
2
+
3
+ A collection of resource-focused controllers for the
4
+ [Happy Web Application Toolkit](https://github.com/hmans/happy).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ # integrate rspec
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new('spec')
7
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/happy-resources/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Hendrik Mans"]
6
+ gem.email = ["hendrik@mans.de"]
7
+ gem.description = %q{A collection of resource-focused controllers for the Happy Web Application Toolkit.}
8
+ gem.summary = %q{A collection of resource-focused controllers for the Happy Web Application Toolkit.}
9
+ gem.homepage = "https://github.com/hmans/happy-resources"
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 = "happy-resources"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Happy::Resources::VERSION
17
+
18
+ gem.add_dependency 'happy', '>= 0.1.0'
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec', '~> 2.8'
22
+ gem.add_development_dependency 'rspec-html-matchers'
23
+ gem.add_development_dependency 'rack-test'
24
+ end
@@ -0,0 +1,9 @@
1
+ require "happy-resources/version"
2
+ require "happy"
3
+ require "happy-resources/resource_controller"
4
+ require "happy-resources/active_model_resource_controller"
5
+
6
+ module Happy
7
+ module Resources
8
+ end
9
+ end
@@ -0,0 +1,97 @@
1
+ module Happy
2
+ module Resources
3
+
4
+ class ActiveModelResourceController < ResourceController
5
+ def root_url
6
+ super(settings[:plural_name])
7
+ end
8
+
9
+ def render_resource_template(name)
10
+ render "#{settings[:plural_name]}/#{name}.html.haml"
11
+ end
12
+
13
+ def resource
14
+ settings[:class]
15
+ end
16
+
17
+ def resource_with_permission_scope(*args)
18
+ permissions.scoped_model(*args, settings[:class])
19
+ end
20
+
21
+ def require_permission!(*args)
22
+ raise "not allowed" unless permissions.can?(*args, settings[:class])
23
+ end
24
+
25
+ def set_plural_variable(v)
26
+ instance_variable_set "@#{settings[:plural_name]}", v
27
+ end
28
+
29
+ def plural_variable
30
+ instance_variable_get "@#{settings[:plural_name]}"
31
+ end
32
+
33
+ def set_singular_variable(v)
34
+ instance_variable_set "@#{settings[:singular_name]}", v
35
+ end
36
+
37
+ def singular_variable
38
+ instance_variable_get "@#{settings[:singular_name]}"
39
+ end
40
+
41
+ def index
42
+ require_permission! :index
43
+ set_plural_variable resource_with_permission_scope(:index).all
44
+ render_resource_template 'index'
45
+ end
46
+
47
+ def show
48
+ require_permission! :show
49
+ set_singular_variable resource_with_permission_scope(:show).find(params['id'])
50
+ render_resource_template 'show'
51
+ end
52
+
53
+ def new
54
+ require_permission! :new
55
+ set_singular_variable resource_with_permission_scope(:new).new(params[settings[:singular_name]], :as => settings[:role])
56
+ render_resource_template 'new'
57
+ end
58
+
59
+ def create
60
+ require_permission! :create
61
+ set_singular_variable resource_with_permission_scope(:create).new(params[settings[:singular_name]], :as => settings[:role])
62
+
63
+ if singular_variable.save
64
+ redirect! singular_variable
65
+ else
66
+ render_resource_template 'new'
67
+ end
68
+ end
69
+
70
+ def edit
71
+ require_permission! :edit
72
+ set_singular_variable resource_with_permission_scope(:edit).find(params['id'])
73
+ render_resource_template 'edit'
74
+ end
75
+
76
+ def update
77
+ require_permission! :update
78
+ set_singular_variable resource_with_permission_scope(:update).find(params['id'])
79
+ singular_variable.assign_attributes params[settings[:singular_name]], :as => settings[:role]
80
+
81
+ if singular_variable.save
82
+ redirect! singular_variable
83
+ else
84
+ render_resource_template 'edit'
85
+ end
86
+ end
87
+
88
+ def route
89
+ settings[:singular_name] ||= settings[:class].to_s.tableize.singularize
90
+ settings[:plural_name] ||= settings[:class].to_s.tableize.pluralize
91
+
92
+ super
93
+ end
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,21 @@
1
+ module Happy
2
+ module Resources
3
+
4
+ class ResourceController < Happy::Controller
5
+ def route
6
+ on_get('new') { new }
7
+
8
+ on :id do
9
+ on_get { show }
10
+ on_put { update }
11
+ on_delete { destroy }
12
+ on_get('edit') { edit }
13
+ end
14
+
15
+ on_post { create }
16
+ on_get { index }
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Happy
2
+ module Resources
3
+ VERSION = "0.1.0.pre1"
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ module Happy::Resources
4
+ describe "a controller derived from ResourceController" do
5
+ subject do
6
+ class MyApp < ResourceController
7
+ def index ; 'index' ; end
8
+ def new ; 'new' ; end
9
+ def create ; 'create' ; end
10
+ def show ; "show #{params['id']}" ; end
11
+ def edit ; "edit #{params['id']}" ; end
12
+ def update ; "update #{params['id']}" ; end
13
+ def destroy ; "destroy #{params['id']}" ; end
14
+ end
15
+
16
+ MyApp
17
+ end
18
+
19
+ it "dispatches GET / requests to #index" do
20
+ response_for { get '/' }.body.should == 'index'
21
+ end
22
+
23
+ it "dispatches GET /new requests to #new" do
24
+ response_for { get '/new' }.body.should == 'new'
25
+ end
26
+
27
+ it "dispatches POST / requests to #create" do
28
+ response_for { post '/' }.body.should == 'create'
29
+ end
30
+
31
+ it "dispatches GET /:id requests to #show" do
32
+ response_for { get '/123' }.body.should == 'show 123'
33
+ end
34
+
35
+ it "dispatches GET /:id/edit requests to #edit" do
36
+ response_for { get '/123/edit' }.body.should == 'edit 123'
37
+ end
38
+
39
+ it "dispatches PUT /:id requests to #update" do
40
+ response_for { put '/123' }.body.should == 'update 123'
41
+ end
42
+
43
+ it "dispatches DELETE /:id requests to #destroy" do
44
+ response_for { delete '/123' }.body.should == 'destroy 123'
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,24 @@
1
+ SPEC_DIR = File.dirname(__FILE__)
2
+ lib_path = File.expand_path("#{SPEC_DIR}/../lib")
3
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
4
+
5
+ require 'rubygems'
6
+ require 'rack/test'
7
+
8
+ require 'happy-resources'
9
+
10
+ module SpecHelpers
11
+ def app
12
+ subject
13
+ end
14
+
15
+ def response_for
16
+ yield if block_given?
17
+ last_response
18
+ end
19
+ end
20
+
21
+ RSpec.configure do |conf|
22
+ conf.include Rack::Test::Methods
23
+ conf.include SpecHelpers
24
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: happy-resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Hendrik Mans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: happy
16
+ requirement: &70319447663460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70319447663460
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70319447663040 !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: *70319447663040
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70319447662500 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.8'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70319447662500
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-html-matchers
49
+ requirement: &70319447678460 !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: *70319447678460
58
+ - !ruby/object:Gem::Dependency
59
+ name: rack-test
60
+ requirement: &70319447678000 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70319447678000
69
+ description: A collection of resource-focused controllers for the Happy Web Application
70
+ Toolkit.
71
+ email:
72
+ - hendrik@mans.de
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - happy-resources.gemspec
84
+ - lib/happy-resources.rb
85
+ - lib/happy-resources/active_model_resource_controller.rb
86
+ - lib/happy-resources/resource_controller.rb
87
+ - lib/happy-resources/version.rb
88
+ - spec/resource_controller_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/hmans/happy-resources
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>'
106
+ - !ruby/object:Gem::Version
107
+ version: 1.3.1
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.11
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: A collection of resource-focused controllers for the Happy Web Application
114
+ Toolkit.
115
+ test_files:
116
+ - spec/resource_controller_spec.rb
117
+ - spec/spec_helper.rb
118
+ has_rdoc: