load_resources 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95eb82094091dea100ec8c889c2a6164fa4933a1
4
- data.tar.gz: a5a1046e7678ef24dcd5c1476b7678ef117e97c7
3
+ metadata.gz: a01fda3fb1c32b4134da3536cbe4bf69453bcfaa
4
+ data.tar.gz: 374cfe9390e363e49072248b18aa1d208f2dc9c6
5
5
  SHA512:
6
- metadata.gz: 89025822b49da3d72d9be3e78bfbd4dd4cfb06be4cd58c8ac15bcc128027e458e3f9dc937781cb25758c8a60fc4c5a07c81f1850b094b3cb38450ea10795c131
7
- data.tar.gz: 2a82cda96e37d01f54c37e252bce2924308369310fbf5baf4764066a8cb0a428f1de86c7495dadf5de3570c9f296f98de56e395d89d54ddae03c3eb8ed5ede1a
6
+ metadata.gz: b66151e0becfae73a78a2031709d9a41efcd95180737a47a35b3bf376313ed0bcdbeddb48c355a508e6f6c807dc37d067d02af975ee88d1089227970e84825ee
7
+ data.tar.gz: 7dc5626057f9f8ede759cbd381a9c9f8c9af732dc986e4a1f18987a11c47d65da96f110001e98b66ffd82aa8bf24c5ba3f9369ff2a4dcc8e49a20a718cfdcad1
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # LoadResources
2
2
 
3
- TODO: Write a gem description
4
-
5
3
  ## Installation
6
4
 
7
5
  Add this line to your application's Gemfile:
@@ -19,8 +17,24 @@ Or install it yourself as:
19
17
  $ gem install load_resources
20
18
 
21
19
  ## Usage
20
+ Include load resources in controller
21
+ include LoadResources
22
+
23
+ for all crud actions
24
+
25
+ automatic_resource :user
26
+
27
+ for specify actions
28
+ automatic_resource :resource_name, only: [:new, :create]
29
+ automatic_resource :resource_name, except: [:new, :create]
30
+
31
+
32
+ TODO
33
+ for non crud actions.
34
+
35
+ automatic_resource :resource_name, only: [:nova_action], options: [:new, :all, :find ou find: {key: find_id}, conditions: {name: 'seila'} ]
36
+ automatic_resource [:resource_name, :resource_name2], only: [:nova_action], options: [:new, :all, :find ou find: {key: find_id}, conditions: {name: 'seila'} ]
22
37
 
23
- TODO: Write usage instructions here
24
38
 
25
39
  ## Contributing
26
40
 
@@ -1,3 +1,3 @@
1
1
  module LoadResources
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,45 +2,84 @@ require "load_resources/version"
2
2
 
3
3
  module LoadResources
4
4
  extend ActiveSupport::Concern
5
+ include ActiveSupport::Callbacks
5
6
 
6
- included do
7
- before_action :load_resource, only: [:show, :edit, :destroy, :update]
8
- before_action :new_resource, only: [:new, :create]
9
- before_action :load_resources, only: :index
7
+ def self.included(base)
8
+ base.extend ClassMethods
10
9
  end
11
10
 
12
- def automatic_resources(resource_names = {}, actions = {})
11
+ module ClassMethods
12
+ def automatic_resource(resource_name, actions = nil, class_name = nil)
13
+ actions ||= default_actions
14
+ new_resource_callback(resource_name, actions)
15
+ collection_resource_callback(resource_name, actions)
16
+ single_resource_callback(resource_name, actions)
17
+ end
13
18
 
14
- end
19
+ def collection_resource_callback(resource_name, actions)
20
+ collection_actions = (actions & collection_resource_actions)
21
+ if collection_actions.present?
22
+ before_action(only: collection_actions) do
23
+ load_resources(resource_name)
24
+ end
25
+ end
26
+ end
27
+
28
+ def new_resource_callback(resource_name, actions)
29
+ new_instance_actions = (actions & new_resource_actions)
30
+ if new_instance_actions.present?
31
+ before_action(only: new_instance_actions) do
32
+ new_resource(resource_name)
33
+ end
34
+ end
35
+ end
36
+
37
+ def single_resource_callback(resource_name, actions)
38
+ single_actions = (actions & single_resource_actions)
39
+ if single_actions.present?
40
+ before_action(only: single_actions) do
41
+ load_resource(resource_name)
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def single_resource_actions
49
+ [:show, :edit, :destroy, :update]
50
+ end
15
51
 
16
- # criar metodo para fazer o load ou o new do resource parecido com o before_action
17
- # automatic_resource :resource_name, only: [:new, :create]
18
- # automatic_resource :resource_name, except: [:new, :create]
19
- # automatic_resource :crud,
20
- # poder especificar o tipo do resource se é um new, um find simples, all, ou a conditions
21
- # automatic_resource :resource_name, only: [:nova_action], options: [:new, :all, :find ou find: {key: find_id}, conditions: {name: 'seila'} ]
22
- # automatic_resource [:resource_name, :resource_name2], only: [:nova_action], options: [:new, :all, :find ou find: {key: find_id}, conditions: {name: 'seila'} ]
52
+ def new_resource_actions
53
+ [:new, :create]
54
+ end
23
55
 
24
- def load_resources
25
- instance_variable_set resource_instance_name.pluralize, resource_class.all
56
+ def collection_resource_actions
57
+ [:index]
58
+ end
59
+
60
+ def default_actions
61
+ [:index, :new, :edit, :create, :update, :destroy]
62
+ end
63
+ end
64
+
65
+ def variable_name(resource_name)
66
+ ("@#{resource_name.to_s}")
26
67
  end
27
68
 
28
- def new_resource
29
- binding.pry
30
- instance_variable_set resource_instance_name, resource_class.new
69
+ def load_resources(resource_name)
70
+ instance_variable_set variable_name(resource_name), resource_class(resource_name).all
31
71
  end
32
72
 
33
- def load_resource
34
- clazz = resource_class
35
- instance_variable_set resource_instance_name,
36
- clazz.find_by(id: params[:id])
73
+ def new_resource(resource_name)
74
+ instance_variable_set variable_name(resource_name), resource_class(resource_name).new
37
75
  end
38
76
 
39
- def resource_instance_name
40
- "@#{params['controller'].singularize}"
77
+ def load_resource(resource_name)
78
+ instance_variable_set variable_name(resource_name), resource_class(resource_name).find_by(id: params[:id])
41
79
  end
42
80
 
43
- def resource_class
44
- Object.const_get(params['controller'].classify)
81
+ def resource_class(resource_name)
82
+ Object.const_get(resource_name.to_s.classify)
45
83
  end
84
+
46
85
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: load_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renan Oronfle
@@ -30,7 +30,7 @@ cert_chain:
30
30
  G/deCkh3WDUtBeEKSz3F0EeN+83rUs3YsHC8BLBWYijdvcBPipD8GZgTqnKW9aSY
31
31
  h+9WLcjUsXPxXVT9F0v/eS3W3q334RUrEpGGbTFJyt+W4K+t
32
32
  -----END CERTIFICATE-----
33
- date: 2016-02-18 00:00:00.000000000 Z
33
+ date: 2016-02-19 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: bundler
metadata.gz.sig CHANGED
Binary file