lazy_crud 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32871f8c7fcf29804cba0f25c3ab1fa585cf0516
4
- data.tar.gz: af379a9727d4b46fe0451da95a133512f04ff925
3
+ metadata.gz: fd3014b37d04ad90643b0d6a76b4226ff6744d95
4
+ data.tar.gz: 570152da372dbb3901ff85ea3d31f6a50b6e5177
5
5
  SHA512:
6
- metadata.gz: d23a9e140f80b25061906919c41bb190927b29d7658e3b619545baf2fade1958d0b36c6ec3f297d184591d7aff33bc369992b7959dd5cbf33ef19e0e760a2a37
7
- data.tar.gz: 556c8e9fe7e60af0ade37b871e2bca0b783e14252821a6bb8a0c21dcb498fdc4bd223b026485428b5c9e280e250872d1f914c277bad95a1c5dce4ef042713705
6
+ metadata.gz: 97e86b3f77c7539212b949b6ce4961f61fa1bc871e3421aa402f8f988b5a0a2feb2fe18134978e18ab164741a81f82133371b80b6e65d753f97d8e33f7d7ddc8
7
+ data.tar.gz: abf440e50d917d27016ba78fbe04fe374301c3638ed542cf78a1d408426a77e25e38f5f6597c9f85af074065e69f536f28aee041086d7aba50504c7391b3d340
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lazy_crud (0.9.2)
4
+ lazy_crud (0.9.4)
5
5
  activesupport
6
6
  i18n
7
7
 
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # lazy_crud
1
+ # lazy_crud [![Join the chat at https://gitter.im/NullVoxPopuli/lazy_crud](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/NullVoxPopuli/lazy_crud?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2
+
2
3
  Lazy way to implement common actions in controllers in Rails
3
4
 
4
5
  [![docs](https://img.shields.io/badge/docs-yardoc-blue.svg?style=flat-square)](http://www.rubydoc.info/github/NullVoxPopuli/lazy_crud)
@@ -35,31 +36,61 @@ Terminal
35
36
 
36
37
  ### Basic setup
37
38
 
39
+ Include all of the basic actions, `create`, `edit`, `destroy`, etc
40
+
38
41
  class SomeObjectController < ApplicationController
39
42
  include LazyCrud
40
43
 
41
- # At a bare minimum, you'll need to specify the
42
- # class the controller is acting upon.
44
+ At a bare minimum, you'll need to specify the class the controller is acting upon.
45
+
43
46
  set_resource SomeObject
44
47
 
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
48
+
49
+ **Optional**
50
+
51
+ This is for in the case of you having nested routes, and want to scope
52
+ `SomeObject` to its parent object.
53
+
54
+ For example: `/event/:event_id/discounts/:id`
55
+ Event is the parent object and Discount is the resource
56
+
57
+ Note that there must be an `@event` instance variable set.
58
+ See specs for details.
59
+
54
60
  set_resource_parent Event
55
61
 
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
62
+ **Sort of Optional**
63
+
64
+ If you want to be able to update / create objects, you'll want to
65
+ specify what parameters are allowed to be set.
66
+ This uses strong parameters.
67
+
60
68
  set_param_whitelist(:name, :amount)
69
+
70
+ # ... other controller stuff
61
71
  end
62
72
 
73
+
74
+ ### CRUD-hooks
75
+
76
+ Sometimes you may want to manually assign attributes to an object before saving, such as current_user.
77
+
78
+ There are two ways to do this:
79
+
80
+ def before_create
81
+ @resource.user = current_user
82
+ end
83
+
84
+ or if you are wanting to have ruby throw an error if you spelled something wrong
85
+
86
+ before_create ->(resource){ resource.user = current_user }
87
+
88
+ the error thrown will be NoMethodError
89
+
90
+ Available CRUD-hooks are: `before_create`, `before_update`, `before_destroy`
91
+
92
+ Each hook can be called multiple times, and they will be invoked in the order they were defined. If the `def` method is used, it will be invoked last.
93
+
63
94
  ## Contributing
64
95
 
65
96
  1. Fork the project
@@ -1,3 +1,3 @@
1
1
  module LazyCrud
2
- VERSION = "0.9.3"
2
+ VERSION = "0.9.4"
3
3
  end
data/lib/lazy_crud.rb CHANGED
@@ -5,11 +5,18 @@ require 'lazy_crud/version'
5
5
  module LazyCrud
6
6
  extend ActiveSupport::Concern
7
7
 
8
+ ACTIONS_WITH_HOOKS = [:create, :update, :destroy]
9
+
8
10
  included do
9
11
  class_attribute :resource_class
10
12
  class_attribute :parent_class
11
13
  class_attribute :param_whitelist
12
14
 
15
+ # crud hooks
16
+ class_attribute :before_create_hooks
17
+ class_attribute :before_update_hooks
18
+ class_attribute :before_destroy_hooks
19
+ # setting instance variables for actions and views
13
20
  before_action :set_resource, only: [:show, :edit, :update, :destroy]
14
21
  before_action :set_resource_instance, only: [:show, :edit, :update, :destroy]
15
22
  end
@@ -35,6 +42,35 @@ module LazyCrud
35
42
  self.param_whitelist = param_list
36
43
  end
37
44
 
45
+ ACTIONS_WITH_HOOKS.each do |action|
46
+ # adds a lambda to the hook array
47
+ define_method("before_#{action}") do |func|
48
+ hook_list = self.send("before_#{action}_hooks")
49
+ hook_list ||= []
50
+ hook_list << func
51
+
52
+ self.send("before_#{action}_hooks=", hook_list)
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ ACTIONS_WITH_HOOKS.each do |action|
59
+ # runs all of the hooks
60
+ define_method("run_before_#{action}_hooks") do
61
+ hook_list = self.send("before_#{action}_hooks")
62
+
63
+ if hook_list
64
+ hook_list.each do |hook|
65
+ hook.call(@resource)
66
+ end
67
+ end
68
+
69
+ # run the before action method if it exists
70
+ if respond_to?("before_#{action}")
71
+ self.send("before_#{action}")
72
+ end
73
+ end
38
74
  end
39
75
 
40
76
  def index
@@ -59,6 +95,7 @@ module LazyCrud
59
95
  # ensure we can still use model name-based instance variables
60
96
  set_resource_instance
61
97
 
98
+ run_before_create_hooks
62
99
  if @resource.save
63
100
  flash[:notice] = "#{resource_name} has been created."
64
101
  redirect_to action: :index
@@ -68,6 +105,7 @@ module LazyCrud
68
105
  end
69
106
 
70
107
  def update
108
+ run_before_update_hooks
71
109
  if @resource.update(resource_params)
72
110
  redirect_to action: :index
73
111
  else
@@ -76,6 +114,7 @@ module LazyCrud
76
114
  end
77
115
 
78
116
  def destroy
117
+ run_before_destroy_hooks
79
118
  @resource.destroy
80
119
 
81
120
  flash[:notice] = "#{resource_name} has been deleted."
@@ -0,0 +1,51 @@
1
+ require 'rails_helper'
2
+
3
+ describe EventsController, type: :controller do
4
+
5
+ describe 'before_create' do
6
+ it 'has 2 hooks' do
7
+ hooks = controller.class.before_create_hooks
8
+ expect(hooks.count).to eq 2
9
+ end
10
+
11
+ it 'calls before_create hooks' do
12
+
13
+ expect(controller).to receive(:before_create)
14
+ post :create, event: build(:event).attributes
15
+ end
16
+
17
+ it 'evaluates the before_create hooks' do
18
+ attributes = build(:event).attributes
19
+ post :create, event: attributes
20
+
21
+ name = attributes["name"].upcase
22
+ expected = name + " " + name
23
+ actual = assigns(:event).name
24
+ expect(actual).to eq expected
25
+ end
26
+ end
27
+
28
+ describe 'before_update' do
29
+
30
+ it 'calls before_update hooks' do
31
+ event = create(:event)
32
+ put :update, id: event.id, event: {}
33
+ actual = assigns(:event).user_id
34
+ expect(actual).to eq -1
35
+ end
36
+
37
+ end
38
+
39
+ describe 'before_destroy' do
40
+
41
+ it 'calls before_destroy hooks' do
42
+ event = create(:event)
43
+ expect(event.created_at).to_not eq nil
44
+ delete :destroy, id: event.id
45
+ expect(assigns(:event).created_at).to eq nil
46
+
47
+ end
48
+ end
49
+
50
+
51
+ end
@@ -1,58 +1,18 @@
1
1
  class EventsController < ApplicationController
2
- before_action :set_event, only: [:show, :edit, :update, :destroy]
2
+ include LazyCrud
3
3
 
4
- # GET /events
5
- def index
6
- @events = Event.all
7
- end
8
-
9
- # GET /events/1
10
- def show
11
- end
12
-
13
- # GET /events/new
14
- def new
15
- @event = Event.new
16
- end
4
+ set_resource Event
5
+ set_param_whitelist(:name)
17
6
 
18
- # GET /events/1/edit
19
- def edit
20
- end
21
-
22
- # POST /events
23
- def create
24
- @event = Event.new(event_params)
7
+ before_create ->(resource){ resource.name = resource.name.upcase }
8
+ before_create ->(resource){}
25
9
 
26
- if @event.save
27
- redirect_to @event, notice: 'Event was successfully created.'
28
- else
29
- render :new
30
- end
31
- end
32
-
33
- # PATCH/PUT /events/1
34
- def update
35
- if @event.update(event_params)
36
- redirect_to @event, notice: 'Event was successfully updated.'
37
- else
38
- render :edit
39
- end
40
- end
10
+ before_update ->(resource){ resource.user_id = -1 }
11
+ before_destroy ->(resource){ resource.created_at = nil }
41
12
 
42
- # DELETE /events/1
43
- def destroy
44
- @event.destroy
45
- redirect_to events_url, notice: 'Event was successfully destroyed.'
13
+ def before_create
14
+ @resource.name = @resource.name + " " + @resource.name
46
15
  end
47
16
 
48
- private
49
- # Use callbacks to share common setup or constraints between actions.
50
- def set_event
51
- @event = Event.find(params[:id])
52
- end
53
17
 
54
- # Only allow a trusted parameter "white list" through.
55
- def event_params
56
- params[:event]
57
- end
58
18
  end
@@ -4,6 +4,11 @@ describe LazyCrud do
4
4
 
5
5
  class Example < ActionController::Base
6
6
  include LazyCrud
7
+
8
+ before_create -> (resource){ 2+2 }
9
+ before_update -> (resource){ 2+2 }
10
+ before_destroy -> (resource){ 2+2 }
11
+
7
12
  end
8
13
 
9
14
 
@@ -19,4 +24,24 @@ describe LazyCrud do
19
24
  end
20
25
 
21
26
 
27
+ context 'calling a hook adds it to the hook list' do
28
+
29
+ it 'before_create appends to the hook list' do
30
+ hooks = Example.before_create_hooks
31
+ expect(hooks.count).to eq 1
32
+ end
33
+
34
+ it 'before_update appends to the hook list' do
35
+ hooks = Example.before_update_hooks
36
+ expect(hooks.count).to eq 1
37
+ end
38
+
39
+ it 'before_destroy appends to the hook list' do
40
+ hooks = Example.before_destroy_hooks
41
+ expect(hooks.count).to eq 1
42
+ end
43
+
44
+ end
45
+
46
+
22
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_crud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - L. Preston Sego III
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-10 00:00:00.000000000 Z
11
+ date: 2015-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -197,6 +197,7 @@ files:
197
197
  - lib/lazy_crud.rb
198
198
  - lib/lazy_crud/version.rb
199
199
  - spec/integration/discounts_controller_spec.rb
200
+ - spec/integration/events_controller_spec.rb
200
201
  - spec/integration/users_controller_spec.rb
201
202
  - spec/rails_helper.rb
202
203
  - spec/spec_helper.rb
@@ -297,9 +298,10 @@ rubyforge_project:
297
298
  rubygems_version: 2.4.6
298
299
  signing_key:
299
300
  specification_version: 4
300
- summary: LazyCrud-0.9.3
301
+ summary: LazyCrud-0.9.4
301
302
  test_files:
302
303
  - spec/integration/discounts_controller_spec.rb
304
+ - spec/integration/events_controller_spec.rb
303
305
  - spec/integration/users_controller_spec.rb
304
306
  - spec/rails_helper.rb
305
307
  - spec/spec_helper.rb