lazy_crud 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +7 -0
  3. data/.gitignore +37 -0
  4. data/.travis.yml +15 -0
  5. data/Gemfile +4 -0
  6. data/Gemfile.lock +142 -0
  7. data/LICENSE +22 -0
  8. data/README.md +69 -0
  9. data/Rakefile +1 -0
  10. data/config/locales/en.yml +11 -0
  11. data/lazy_crud.gemspec +42 -0
  12. data/lib/lazy_crud/version.rb +3 -0
  13. data/lib/lazy_crud.rb +161 -0
  14. data/spec/integration/discounts_controller_spec.rb +95 -0
  15. data/spec/rails_helper.rb +15 -0
  16. data/spec/spec_helper.rb +39 -0
  17. data/spec/support/factories.rb +18 -0
  18. data/spec/support/factory_girl.rb +7 -0
  19. data/spec/support/rails_app/Rakefile +6 -0
  20. data/spec/support/rails_app/app/assets/images/.keep +0 -0
  21. data/spec/support/rails_app/app/assets/javascripts/application.js +16 -0
  22. data/spec/support/rails_app/app/assets/javascripts/some_resources.js +2 -0
  23. data/spec/support/rails_app/app/assets/javascripts/users.js +2 -0
  24. data/spec/support/rails_app/app/assets/stylesheets/application.css +15 -0
  25. data/spec/support/rails_app/app/assets/stylesheets/scaffold.css +56 -0
  26. data/spec/support/rails_app/app/assets/stylesheets/some_resources.css +4 -0
  27. data/spec/support/rails_app/app/assets/stylesheets/users.css +4 -0
  28. data/spec/support/rails_app/app/controllers/application_controller.rb +14 -0
  29. data/spec/support/rails_app/app/controllers/discounts_controller.rb +14 -0
  30. data/spec/support/rails_app/app/controllers/events_controller.rb +58 -0
  31. data/spec/support/rails_app/app/controllers/users_controller.rb +6 -0
  32. data/spec/support/rails_app/app/helpers/application_helper.rb +2 -0
  33. data/spec/support/rails_app/app/helpers/events_helper.rb +2 -0
  34. data/spec/support/rails_app/app/helpers/users_helper.rb +2 -0
  35. data/spec/support/rails_app/app/models/collaboration.rb +16 -0
  36. data/spec/support/rails_app/app/models/concerns/.keep +0 -0
  37. data/spec/support/rails_app/app/models/discount.rb +5 -0
  38. data/spec/support/rails_app/app/models/event.rb +4 -0
  39. data/spec/support/rails_app/app/models/user.rb +9 -0
  40. data/spec/support/rails_app/app/views/discounts/edit.html.erb +0 -0
  41. data/spec/support/rails_app/app/views/discounts/index.html.erb +0 -0
  42. data/spec/support/rails_app/app/views/discounts/new.html.erb +0 -0
  43. data/spec/support/rails_app/app/views/discounts/show.html.erb +0 -0
  44. data/spec/support/rails_app/app/views/events/_form.html.erb +17 -0
  45. data/spec/support/rails_app/app/views/events/edit.html.erb +6 -0
  46. data/spec/support/rails_app/app/views/events/index.html.erb +25 -0
  47. data/spec/support/rails_app/app/views/events/new.html.erb +5 -0
  48. data/spec/support/rails_app/app/views/events/show.html.erb +4 -0
  49. data/spec/support/rails_app/app/views/layouts/application.html.erb +14 -0
  50. data/spec/support/rails_app/app/views/users/_form.html.erb +17 -0
  51. data/spec/support/rails_app/app/views/users/edit.html.erb +6 -0
  52. data/spec/support/rails_app/app/views/users/index.html.erb +25 -0
  53. data/spec/support/rails_app/app/views/users/new.html.erb +5 -0
  54. data/spec/support/rails_app/app/views/users/show.html.erb +4 -0
  55. data/spec/support/rails_app/bin/bundle +3 -0
  56. data/spec/support/rails_app/bin/rails +8 -0
  57. data/spec/support/rails_app/bin/rake +4 -0
  58. data/spec/support/rails_app/config/application.rb +30 -0
  59. data/spec/support/rails_app/config/boot.rb +3 -0
  60. data/spec/support/rails_app/config/database.yml +25 -0
  61. data/spec/support/rails_app/config/environment.rb +5 -0
  62. data/spec/support/rails_app/config/environments/development.rb +41 -0
  63. data/spec/support/rails_app/config/environments/production.rb +79 -0
  64. data/spec/support/rails_app/config/environments/test.rb +42 -0
  65. data/spec/support/rails_app/config/initializers/assets.rb +11 -0
  66. data/spec/support/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  67. data/spec/support/rails_app/config/initializers/cookies_serializer.rb +3 -0
  68. data/spec/support/rails_app/config/initializers/filter_parameter_logging.rb +4 -0
  69. data/spec/support/rails_app/config/initializers/inflections.rb +16 -0
  70. data/spec/support/rails_app/config/initializers/mime_types.rb +4 -0
  71. data/spec/support/rails_app/config/initializers/session_store.rb +3 -0
  72. data/spec/support/rails_app/config/initializers/wrap_parameters.rb +14 -0
  73. data/spec/support/rails_app/config/locales/en.yml +23 -0
  74. data/spec/support/rails_app/config/routes.rb +67 -0
  75. data/spec/support/rails_app/config/secrets.yml +22 -0
  76. data/spec/support/rails_app/config.ru +0 -0
  77. data/spec/support/rails_app/db/development.sqlite3 +0 -0
  78. data/spec/support/rails_app/db/migrate/20141231134904_create_users.rb +8 -0
  79. data/spec/support/rails_app/db/migrate/20150102225507_create_events.rb +9 -0
  80. data/spec/support/rails_app/db/migrate/20150104171110_create_discounts.rb +13 -0
  81. data/spec/support/rails_app/db/schema.rb +38 -0
  82. data/spec/support/rails_app/db/seeds.rb +7 -0
  83. data/spec/support/rails_app/db/test.sqlite3 +0 -0
  84. data/spec/support/rails_app/public/404.html +67 -0
  85. data/spec/support/rails_app/public/422.html +67 -0
  86. data/spec/support/rails_app/public/500.html +66 -0
  87. data/spec/support/rails_app/public/favicon.ico +0 -0
  88. data/spec/support/rails_app/public/robots.txt +5 -0
  89. data/spec/support/rails_app/reset-db +1 -0
  90. data/spec/unit/lazy_crud_spec.rb +0 -0
  91. metadata +377 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02b7f6af2ad8a5c3867f8c9702a7992f5ec4101f
4
+ data.tar.gz: ec1598c7d0b7b5e9f854e8971f044fdba82ccb21
5
+ SHA512:
6
+ metadata.gz: 242d7160db8d6066cbc5e6676cf46c192167eb57be0b32ad191691b7a6cf10f5ff0a4888e66d50c1eff104ea2e8faf4826355f916acd609b1c9f6a87296c0799
7
+ data.tar.gz: 06e045db9a2878494cd7195a279a09e812c53fb56b22dffe7a414dc16eda05600534185a3d53589b5dc482d750bd6796a95c60a6dd2eb7160794705245517e55
data/.codeclimate.yml ADDED
@@ -0,0 +1,7 @@
1
+ languages:
2
+ Ruby: true
3
+ JavaScript: false
4
+ PHP: false
5
+ Python: false
6
+ exclude_paths:
7
+ - spec/
data/.gitignore ADDED
@@ -0,0 +1,37 @@
1
+ *.rbc
2
+ *.gem
3
+ capybara-*.html
4
+ .rspec
5
+ /log
6
+ *.log
7
+ /tmp
8
+ /db/*.sqlite3
9
+ /db/*.sqlite3-journal
10
+ /public/system
11
+ /coverage/
12
+ /spec/tmp
13
+ **.orig
14
+ rerun.txt
15
+ pickle-email-*.html
16
+
17
+ # TODO Comment out these rules if you are OK with secrets being uploaded to the repo
18
+ config/initializers/secret_token.rb
19
+ config/secrets.yml
20
+
21
+ ## Environment normalisation:
22
+ /.bundle
23
+ /vendor/bundle
24
+
25
+ # these should all be checked in to normalise the environment:
26
+ # Gemfile.lock, .ruby-version, .ruby-gemset
27
+
28
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
29
+ .rvmrc
30
+
31
+ # if using bower-rails ignore default bower_components path bower.json files
32
+ /vendor/assets/bower_components
33
+ *.bowerrc
34
+ bower.json
35
+
36
+ # Ignore pow environment settings
37
+ .powenv
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ bundler_args: --without guard
3
+ rvm:
4
+ - "2.0"
5
+ - "2.1"
6
+ - "2.2"
7
+ - ruby-head
8
+ script: "bundle exec rspec"
9
+ addons:
10
+ code_climate:
11
+ repo_token: 4ce664811485b705e1c44aa00ff41f207347874b2e840e38f27880f8ebaf96e5
12
+ branches:
13
+ only: master
14
+ notifications:
15
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in authorizable.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,142 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lazy_crud (0.9.0)
5
+ activesupport (>= 3.0.0)
6
+ i18n
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.1.10)
12
+ actionpack (= 4.1.10)
13
+ actionview (= 4.1.10)
14
+ mail (~> 2.5, >= 2.5.4)
15
+ actionpack (4.1.10)
16
+ actionview (= 4.1.10)
17
+ activesupport (= 4.1.10)
18
+ rack (~> 1.5.2)
19
+ rack-test (~> 0.6.2)
20
+ actionview (4.1.10)
21
+ activesupport (= 4.1.10)
22
+ builder (~> 3.1)
23
+ erubis (~> 2.7.0)
24
+ activemodel (4.1.10)
25
+ activesupport (= 4.1.10)
26
+ builder (~> 3.1)
27
+ activerecord (4.1.10)
28
+ activemodel (= 4.1.10)
29
+ activesupport (= 4.1.10)
30
+ arel (~> 5.0.0)
31
+ activesupport (4.1.10)
32
+ i18n (~> 0.6, >= 0.6.9)
33
+ json (~> 1.7, >= 1.7.7)
34
+ minitest (~> 5.1)
35
+ thread_safe (~> 0.1)
36
+ tzinfo (~> 1.1)
37
+ arel (5.0.1.20140414130214)
38
+ awesome_print (1.6.1)
39
+ builder (3.2.2)
40
+ byebug (4.0.5)
41
+ columnize (= 0.9.0)
42
+ codeclimate-test-reporter (0.4.7)
43
+ simplecov (>= 0.7.1, < 1.0.0)
44
+ coderay (1.1.0)
45
+ columnize (0.9.0)
46
+ diff-lcs (1.2.5)
47
+ docile (1.1.5)
48
+ erubis (2.7.0)
49
+ factory_girl (4.5.0)
50
+ activesupport (>= 3.0.0)
51
+ factory_girl_rails (4.5.0)
52
+ factory_girl (~> 4.5.0)
53
+ railties (>= 3.0.0)
54
+ i18n (0.7.0)
55
+ json (1.8.2)
56
+ mail (2.6.3)
57
+ mime-types (>= 1.16, < 3)
58
+ method_source (0.8.2)
59
+ mime-types (2.5)
60
+ minitest (5.6.1)
61
+ paranoia (2.1.1)
62
+ activerecord (~> 4.0)
63
+ pry (0.10.1)
64
+ coderay (~> 1.1.0)
65
+ method_source (~> 0.8.1)
66
+ slop (~> 3.4)
67
+ pry-byebug (3.1.0)
68
+ byebug (~> 4.0)
69
+ pry (~> 0.10)
70
+ rack (1.5.2)
71
+ rack-test (0.6.3)
72
+ rack (>= 1.0)
73
+ rails (4.1.10)
74
+ actionmailer (= 4.1.10)
75
+ actionpack (= 4.1.10)
76
+ actionview (= 4.1.10)
77
+ activemodel (= 4.1.10)
78
+ activerecord (= 4.1.10)
79
+ activesupport (= 4.1.10)
80
+ bundler (>= 1.3.0, < 2.0)
81
+ railties (= 4.1.10)
82
+ sprockets-rails (~> 2.0)
83
+ railties (4.1.10)
84
+ actionpack (= 4.1.10)
85
+ activesupport (= 4.1.10)
86
+ rake (>= 0.8.7)
87
+ thor (>= 0.18.1, < 2.0)
88
+ rake (10.4.2)
89
+ rspec (3.2.0)
90
+ rspec-core (~> 3.2.0)
91
+ rspec-expectations (~> 3.2.0)
92
+ rspec-mocks (~> 3.2.0)
93
+ rspec-core (3.2.3)
94
+ rspec-support (~> 3.2.0)
95
+ rspec-expectations (3.2.1)
96
+ diff-lcs (>= 1.2.0, < 2.0)
97
+ rspec-support (~> 3.2.0)
98
+ rspec-mocks (3.2.1)
99
+ diff-lcs (>= 1.2.0, < 2.0)
100
+ rspec-support (~> 3.2.0)
101
+ rspec-rails (3.2.1)
102
+ actionpack (>= 3.0, < 4.3)
103
+ activesupport (>= 3.0, < 4.3)
104
+ railties (>= 3.0, < 4.3)
105
+ rspec-core (~> 3.2.0)
106
+ rspec-expectations (~> 3.2.0)
107
+ rspec-mocks (~> 3.2.0)
108
+ rspec-support (~> 3.2.0)
109
+ rspec-support (3.2.2)
110
+ simplecov (0.10.0)
111
+ docile (~> 1.1.0)
112
+ json (~> 1.8)
113
+ simplecov-html (~> 0.10.0)
114
+ simplecov-html (0.10.0)
115
+ slop (3.6.0)
116
+ sprockets (3.0.2)
117
+ rack (~> 1.0)
118
+ sprockets-rails (2.2.4)
119
+ actionpack (>= 3.0)
120
+ activesupport (>= 3.0)
121
+ sprockets (>= 2.8, < 4.0)
122
+ sqlite3 (1.3.10)
123
+ thor (0.19.1)
124
+ thread_safe (0.3.5)
125
+ tzinfo (1.2.2)
126
+ thread_safe (~> 0.1)
127
+
128
+ PLATFORMS
129
+ ruby
130
+
131
+ DEPENDENCIES
132
+ awesome_print
133
+ bundler
134
+ codeclimate-test-reporter
135
+ factory_girl_rails (~> 4.4)
136
+ lazy_crud!
137
+ paranoia
138
+ pry-byebug
139
+ rails (>= 4)
140
+ rspec
141
+ rspec-rails
142
+ sqlite3
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 L. Preston Sego III
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # lazy_crud
2
+ Lazy way to implement common actions in controllers in Rails
3
+
4
+ [![docs](https://img.shields.io/badge/docs-yardoc-blue.svg?style=flat-square)](http://www.rubydoc.info/github/NullVoxPopuli/lazy_crud)
5
+ [![Gem Version](http://img.shields.io/gem/v/lazy_crud.svg?style=flat-square)](http://badge.fury.io/rb/lazy_crud)
6
+ [![Build Status](http://img.shields.io/travis/NullVoxPopuli/lazy_crud.svg?style=flat-square)](https://travis-ci.org/NullVoxPopuli/lazy_crud)
7
+ [![Code Climate](http://img.shields.io/codeclimate/github/NullVoxPopuli/lazy_crud.svg?style=flat-square)](https://codeclimate.com/github/NullVoxPopuli/lazy_crud)
8
+ [![Test Coverage](http://img.shields.io/codeclimate/coverage/github/NullVoxPopuli/lazy_crud.svg?style=flat-square)](https://codeclimate.com/github/NullVoxPopuli/lazy_crud)
9
+ [![Dependency Status](http://img.shields.io/gemnasium/NullVoxPopuli/lazy_crud.svg?style=flat-square)](https://gemnasium.com/NullVoxPopuli/lazy_crud)
10
+
11
+
12
+ ## Features
13
+
14
+ - Minimal Controller Coding
15
+ - Resource Can be Scoped to Parent Resource
16
+
17
+ ### TODO
18
+
19
+ - Generic Error Handeling
20
+ - i18n
21
+ - lambdas and procs for inserting custom behavior
22
+
23
+ ## Installation
24
+
25
+ Gemfile
26
+
27
+ gem 'lazy_crud'
28
+
29
+ Terminal
30
+
31
+ gem install lazy_crud
32
+
33
+ ## Configuration
34
+
35
+
36
+ ### Basic setup
37
+
38
+ class SomeObjectController < ApplicationController
39
+ include LazyCrud
40
+
41
+ # At a bare minimum, you'll need to specify the
42
+ # class the controller is acting upon.
43
+ set_resource SomeObject
44
+
45
+ # optional
46
+ # this is for in the case of you having nested routes, and want to scope
47
+ # SomeObject to its parent object.
48
+ # For example: /event/:event_id/discounts/:id
49
+ # Event is the parent object and Discount is the resource
50
+ #
51
+ # Note that there must be an @event instance variable set
52
+ #
53
+ # See specs for details
54
+ set_resource_parent Event
55
+
56
+ # sort of optional
57
+ # if you want to be able to update / create objects, you'll want to
58
+ # specify what parameters are allowed to be set.
59
+ # this uses strong parameters
60
+ set_param_whitelist(:name, :amount)
61
+ end
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork the project
66
+ 2. Create a new, descriptively named branch
67
+ 3. Add Test(s)!
68
+ 4. Commit your proposed changes
69
+ 5. Submit a pull request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ en:
2
+ lazy_crud:
3
+ crud:
4
+ success:
5
+ create: ''
6
+ update: ''
7
+ destroy: ''
8
+ failure:
9
+ create: ''
10
+ update: ''
11
+ destroy: ''
data/lazy_crud.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "lazy_crud/version"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "lazy_crud"
9
+ s.version = LazyCrud::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.license = "MIT"
12
+ s.authors = ["L. Preston Sego III"]
13
+ s.email = "LPSego3+dev@gmail.com"
14
+ s.homepage = "https://github.com/NullVoxPopuli/lazy_crud"
15
+ s.summary = "LazyCrud-#{LazyCrud::VERSION}"
16
+ s.description = "Lazy way to implement common actions in controllers in Rails."
17
+
18
+
19
+ s.files = `git ls-files`.split($/)
20
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
22
+ s.require_paths = ["lib"]
23
+
24
+ s.required_ruby_version = '>= 2.0'
25
+
26
+ s.add_runtime_dependency "activesupport", ">= 3.0.0"
27
+ s.add_runtime_dependency "i18n"
28
+
29
+ # for testing a gem with a rails app (controller specs)
30
+ # https://codingdaily.wordpress.com/2011/01/14/test-a-gem-with-the-rails-3-stack/
31
+ s.add_development_dependency "bundler"
32
+ s.add_development_dependency "rails", ">= 4"
33
+ s.add_development_dependency "factory_girl_rails", "~> 4.4"
34
+ s.add_development_dependency "paranoia"
35
+ s.add_development_dependency "awesome_print"
36
+ s.add_development_dependency "rspec"
37
+ s.add_development_dependency "rspec-rails"
38
+ s.add_development_dependency "sqlite3"
39
+ s.add_development_dependency "pry-byebug"
40
+ s.add_development_dependency "codeclimate-test-reporter"
41
+
42
+ end
@@ -0,0 +1,3 @@
1
+ module LazyCrud
2
+ VERSION = "0.9.0"
3
+ end
data/lib/lazy_crud.rb ADDED
@@ -0,0 +1,161 @@
1
+ require 'active_support'
2
+
3
+ require 'lazy_crud/version'
4
+
5
+ module LazyCrud
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :resource_class
10
+ class_attribute :parent_class
11
+ class_attribute :param_whitelist
12
+
13
+ before_action :set_resource, only: [:show, :edit, :update, :destroy]
14
+ before_action :set_resource_instance, only: [:show, :edit, :update, :destroy]
15
+ end
16
+
17
+ module ClassMethods
18
+
19
+ # all REST actions will take place on an instance of this class
20
+ def set_resource(klass)
21
+ self.resource_class = klass
22
+ end
23
+
24
+ # for scoping the resource
25
+ # useful for nested routes, such as
26
+ # /event/:event_id/package/:package_id
27
+ # where Event would be the parent class and would scope
28
+ # the package
29
+ def set_resource_parent(klass)
30
+ self.parent_class = klass
31
+ end
32
+
33
+ # the list of parameters to allow through the strong parameter filter
34
+ def set_param_whitelist(*param_list)
35
+ self.param_whitelist = param_list
36
+ end
37
+
38
+ end
39
+
40
+ def index
41
+ set_collection_instance
42
+ end
43
+
44
+ def show
45
+ # instance variable set in before_action
46
+ end
47
+
48
+ def new
49
+ set_resource_instance(resource_proxy.new)
50
+ end
51
+
52
+ def edit
53
+ # instance variable set in before_action
54
+ end
55
+
56
+ def create
57
+ @resource = resource_proxy.build(resource_params)
58
+
59
+ # ensure we can still use model name-based instance variables
60
+ set_resource_instance
61
+
62
+ if @resource.save
63
+ flash[:notice] = "#{resource_name} has been created."
64
+ redirect_to action: :index
65
+ else
66
+ render action: :new
67
+ end
68
+ end
69
+
70
+ def update
71
+ if @resource.update(resource_params)
72
+ redirect_to action: :index
73
+ else
74
+ redirect_to action: :edit
75
+ end
76
+ end
77
+
78
+ def destroy
79
+ @resource.destroy
80
+
81
+ flash[:notice] = "#{resource_name} has been deleted."
82
+ redirect_to action: :index
83
+ end
84
+
85
+ # only works if deleting of resources occurs by setting
86
+ # the deleted_at field
87
+ def undestroy
88
+ @resource = resource_proxy(true).find(params[:id])
89
+ set_resource_instance
90
+
91
+ @resource.deleted_at = nil
92
+ @resource.save
93
+
94
+ flash[:notice] = "#{resource_name} has been undeleted"
95
+ redirect_to action: :index
96
+ end
97
+
98
+ private
99
+
100
+ def resource_name
101
+ @resource.try(:name) || @resource.class.name
102
+ end
103
+
104
+ def set_resource
105
+ @resource = resource_proxy.find(params[:id])
106
+ end
107
+
108
+ def resource_params
109
+ params[resource_singular_name].permit(self.class.param_whitelist)
110
+ end
111
+
112
+ # determines if we want to use the parent class if available or
113
+ # if we just use the resource class
114
+ def resource_proxy(with_deleted = false)
115
+ proxy = if parent_instance.present?
116
+ parent_instance.send(resource_plural_name)
117
+ else
118
+ self.class.resource_class
119
+ end
120
+
121
+ if with_deleted and proxy.respond_to?(:with_deleted)
122
+ proxy = proxy.with_deleted
123
+ end
124
+
125
+ proxy
126
+ end
127
+
128
+ # allows all of our views to still use things like
129
+ # @level, @event, @whatever
130
+ # rather than just @resource
131
+ def set_resource_instance(resource = @resource)
132
+ instance_variable_set("@#{resource_singular_name}", resource)
133
+ end
134
+
135
+ # sets the plural instance variable for a collection of objects
136
+ def set_collection_instance
137
+ instance_variable_set("@#{resource_plural_name}", resource_proxy)
138
+ end
139
+
140
+ def parent_instance
141
+ unless @parent
142
+ # e.g.: Event => 'event'
143
+ parent_instance_name = self.class.parent_class.name.underscore
144
+ @parent = instance_variable_get("@#{parent_instance_name}")
145
+ end
146
+
147
+ @parent
148
+ end
149
+
150
+ # e.g.: Event => 'events'
151
+ def resource_plural_name
152
+ @association_name ||= self.class.resource_class.name.tableize
153
+ end
154
+
155
+ # e.g.: Event => 'event'
156
+ # alternatively, @resource.class.name.underscore
157
+ def resource_singular_name
158
+ @singular_name ||= resource_plural_name.singularize
159
+ end
160
+
161
+ end
@@ -0,0 +1,95 @@
1
+ require 'rails_helper'
2
+
3
+ describe DiscountsController, type: :controller do
4
+
5
+ before(:each) do
6
+ @user = create(:user)
7
+ @event = create(:event, user: @user)
8
+ end
9
+
10
+ describe '#index' do
11
+ it 'sets the collection' do
12
+ create(:discount, event: @event)
13
+ get :index, event_id: @event.id
14
+
15
+ expect(assigns(:discounts)).to be_present
16
+ end
17
+ end
18
+
19
+ describe '#show' do
20
+
21
+ it 'gets the resource' do
22
+ discount = create(:discount, event: @event)
23
+ get :show, event_id: @event.id, id: discount.id
24
+
25
+ expect(assigns(:discount)).to eq discount
26
+ end
27
+ end
28
+
29
+ describe '#new' do
30
+ it 'creates an instance of the object' do
31
+ get :new, event_id: @event.id
32
+
33
+ expect(assigns(:discount)).to be_new_record
34
+ end
35
+ end
36
+
37
+ describe '#edit' do
38
+
39
+
40
+ it 'sets the instance of the object' do
41
+ discount = create(:discount, event: @event)
42
+ get :edit, event_id: @event.id, id: discount.id
43
+ expect(assigns(:discount)).to eq discount
44
+ end
45
+ end
46
+
47
+ describe '#create' do
48
+ it 'creates and persists a new object' do
49
+ expect{
50
+ post :create, event_id: @event.id, discount: build(:discount).attributes
51
+ }.to change(Discount, :count).by 1
52
+
53
+ expect(assigns(:discount)).to be_present
54
+ end
55
+ end
56
+
57
+ describe '#update' do
58
+ it 'updates and persists changes' do
59
+ discount = create(:discount, event: @event)
60
+ put :update, event_id: @event.id, id: discount.id,
61
+ discount: { name: "updated" }
62
+
63
+ field = assigns(:discount)
64
+ expect(field.name).to eq "updated"
65
+ end
66
+ end
67
+
68
+
69
+ describe '#destroy' do
70
+ it 'destroys the object' do
71
+ discount = create(:discount, event: @event)
72
+
73
+ expect{
74
+ delete :destroy, event_id: @event.id, id: discount.id
75
+ }.to change(@event.discounts, :count).by -1
76
+
77
+ expect(assigns(:discount).deleted_at).to be_present
78
+ end
79
+ end
80
+
81
+
82
+ describe '#undestroy' do
83
+ it 'un destroys the object' do
84
+ discount = create(:discount, event: @event, deleted_at: Time.now)
85
+
86
+ expect{
87
+ post :undestroy, event_id: @event.id, id: discount.id
88
+ }.to change(@event.discounts, :count).by 1
89
+
90
+ expect(assigns(:discount)).to be_present
91
+ end
92
+ end
93
+
94
+
95
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require 'factory_girl_rails'
3
+ require 'rails/all'
4
+ require 'rspec/rails'
5
+
6
+ ActiveRecord::Migration.maintain_test_schema!
7
+
8
+ require 'support/rails_app/config/environment'
9
+
10
+ # set up db
11
+ # be sure to update the schema if required by doing
12
+ # - cd spec/rails_app
13
+ # - rake db:migrate
14
+ ActiveRecord::Schema.verbose = false
15
+ load "support/rails_app/db/schema.rb" # use db agnostic schema by default
@@ -0,0 +1,39 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require "pry-byebug" # binding.pry to debug!
5
+
6
+ # Coverage
7
+ require "codeclimate-test-reporter"
8
+ ENV['CODECLIMATE_REPO_TOKEN'] = "4ce664811485b705e1c44aa00ff41f207347874b2e840e38f27880f8ebaf96e5"
9
+ CodeClimate::TestReporter.start
10
+
11
+ require 'rspec/autorun'
12
+ require 'factory_girl'
13
+
14
+ # This Gem
15
+ require "lazy_crud"
16
+
17
+ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each {|file|
18
+ # skip the dummy app
19
+ next if file.include?('support/rails_app')
20
+ require file
21
+ }
22
+
23
+ # This file was generated by the `rspec --init` command. Conventionally, all
24
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
25
+ # Require this file using `require "spec_helper"` to ensure that it is only
26
+ # loaded once.
27
+ #
28
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
29
+ RSpec.configure do |config|
30
+ config.treat_symbols_as_metadata_keys_with_true_values = true
31
+ config.run_all_when_everything_filtered = true
32
+ config.filter_run :focus
33
+
34
+ # Run specs in random order to surface order dependencies. If you find an
35
+ # order dependency and want to debug it, you can fix the order by providing
36
+ # the seed, which is printed after each run.
37
+ # --seed 1234
38
+ config.order = 'random'
39
+ end