effective_resources 0.1.1 → 0.2

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: c81fb1d207d2a80098702d0539cce1304db0388c
4
- data.tar.gz: d588a94d7b30875b233d130c798b469e289835ae
3
+ metadata.gz: 042a1e7ebafddc560f55cc76c3b4c4271dc53d56
4
+ data.tar.gz: cd9e12a5646863cac21983f01520b9594a658c86
5
5
  SHA512:
6
- metadata.gz: 3bffe8691feaf152e672de6fc5239fea4aa155b67fbc776d1a408388e1554290b14cdb53da6bd79346725466a167c4b5c9f003e6121e150c491492e924d659c5
7
- data.tar.gz: 77d22eed11ba2e26b1b7a1082141755c64b99fe7a9c5dd7b42be97a81ee1b72e7ad0a2b907461c767d7e9e6e943eab207ba1e78b0c4e08d9b24c16cd8f2e3a8c
6
+ metadata.gz: 041fbf5b5709d5293117f4d88d2f6c389bc4fd8182cd3572b499b520d0e7229e35dd02f3317d59b1e2b40a197b0aa366bd0256566be1b692b956b4cf7ced584f
7
+ data.tar.gz: 3800490cf26e3ef45fffe0b2ebe59aa99684204d3feaa8f7c3d06295d9c8d7a56d6a04b0031499ff0b28f1ea986380d427feb3549d2c4e3aba98820eebf35fe3
data/README.md CHANGED
@@ -26,6 +26,12 @@ Add to your contoller:
26
26
  class PostsController < ApplicationController
27
27
  include Effective::CrudController
28
28
 
29
+ protected
30
+
31
+ def post_scope
32
+ {client_id: current_user.client_id} # Or a symbol
33
+ end
34
+
29
35
  def post_params
30
36
  params.require(:post).permit(:id, :title, :body)
31
37
  end
@@ -10,12 +10,12 @@ module Effective
10
10
 
11
11
  def index
12
12
  @page_title ||= resource_plural_name.titleize
13
- EffectiveResources.authorized?(self, :index, resource_class)
13
+ EffectiveResources.authorized?(self, :index, resource_class.new)
14
14
 
15
15
  self.resources ||= resource_class.all
16
16
 
17
17
  if resource_datatable_class
18
- @datatable ||= resource_datatable_class.new(params[:scopes])
18
+ @datatable ||= resource_datatable_class.new(resource_datatable_attributes, params[:scopes])
19
19
  end
20
20
  end
21
21
 
@@ -87,6 +87,15 @@ module Effective
87
87
 
88
88
  protected
89
89
 
90
+ def resource_redirect_path
91
+ case params[:commit].to_s
92
+ when 'Save' ; send(effective_resource.edit_path, resource)
93
+ when 'Save and Continue' ; send(effective_resource.index_path)
94
+ when 'Save and Add New' ; send(effective_resource.new_path)
95
+ else send(effective_resource.show_path, resource)
96
+ end
97
+ end
98
+
90
99
  def resource # @thing
91
100
  instance_variable_get("@#{resource_name}")
92
101
  end
@@ -103,9 +112,7 @@ module Effective
103
112
  send(:instance_variable_set, "@#{resource_plural_name}", instance)
104
113
  end
105
114
 
106
- def resource_class # Thing
107
- effective_resource.klass
108
- end
115
+ private
109
116
 
110
117
  def resource_name # 'thing'
111
118
  effective_resource.name
@@ -115,6 +122,35 @@ module Effective
115
122
  effective_resource.plural_name
116
123
  end
117
124
 
125
+ # Scoped to resource_scope_method_name
126
+ def resource_class # Thing
127
+ @resource_class ||= (
128
+ if resource_scope_method_name.blank?
129
+ effective_resource.klass
130
+ else
131
+ case (resource_scope = send(resource_scope_method_name))
132
+ when Hash ; effective_resource.klass.where(resource_scope)
133
+ when Symbol ; effective_resource.klass.send(resource_scope)
134
+ when nil ; effective_resource.klass
135
+ else
136
+ raise "expected #{resource_scope_method_name} to return a Hash or Symbol"
137
+ end
138
+ end
139
+ )
140
+ end
141
+
142
+ def resource_datatable_attributes
143
+ return {} unless resource_scope_method_name.present?
144
+
145
+ case (resource_scope = send(resource_scope_method_name))
146
+ when Hash ; resource_scope
147
+ when Symbol ; {resource_scope: true}
148
+ when nil ; {}
149
+ else
150
+ raise "expected #{resource_scope_method_name} to return a Hash or Symbol"
151
+ end
152
+ end
153
+
118
154
  def resource_datatable_class # ThingsDatatable
119
155
  effective_resource.datatable_klass
120
156
  end
@@ -123,17 +159,12 @@ module Effective
123
159
  ["#{resource_name}_params", "#{resource_plural_name}_params", 'permitted_params'].find { |name| respond_to?(name, true) } || 'params'
124
160
  end
125
161
 
126
- def resource_index_path
127
- effective_resource.index_path
162
+ def resource_scope_method_name
163
+ ["#{resource_name}_scope", "#{resource_plural_name}_scope", 'resource_scope', 'default_scope'].find { |name| respond_to?(name, true) }
128
164
  end
129
165
 
130
- def resource_redirect_path
131
- case params[:commit].to_s
132
- when 'Save' ; send(effective_resource.edit_path, resource)
133
- when 'Save and Continue' ; send(effective_resource.index_path)
134
- when 'Save and Add New' ; send(effective_resource.new_path)
135
- else send(effective_resource.show_path, resource)
136
- end
166
+ def resource_index_path
167
+ effective_resource.index_path
137
168
  end
138
169
 
139
170
  private
@@ -10,4 +10,10 @@ module EffectiveResourcesHelper
10
10
  end
11
11
  end
12
12
 
13
+ def simple_form_save(form)
14
+ content_tag(:p, class: 'text-right') do
15
+ form.button(:submit, 'Save', data: { disable_with: 'Saving...' })
16
+ end
17
+ end
18
+
13
19
  end
@@ -3,10 +3,14 @@ module Effective
3
3
  module Associations
4
4
 
5
5
  def belong_tos
6
+ return [] unless klass
7
+
6
8
  @belong_tos ||= klass.reflect_on_all_associations(:belongs_to)
7
9
  end
8
10
 
9
11
  def nested_resources
12
+ return {} unless klass
13
+
10
14
  @nested ||= klass.reflect_on_all_autosave_associations.inject({}) do |hash, reference|
11
15
  hash[reference] = Effective::Resource.new(reference); hash
12
16
  end
@@ -25,6 +25,8 @@ module Effective
25
25
  @written_belong_tos = []
26
26
  @written_scopes = []
27
27
 
28
+ return unless File.exists?(model_file)
29
+
28
30
  Effective::CodeReader.new(model_file) do |reader|
29
31
  first = reader.index { |line| line == '# Attributes' }
30
32
  last = reader.index(from: first) { |line| line.start_with?('#') == false && line.length > 0 } if first
@@ -3,7 +3,7 @@ module Effective
3
3
  module Klass
4
4
 
5
5
  def klass
6
- namespaced_class_name.safe_constantize || class_name.safe_constantize || name.constantize
6
+ namespaced_class_name.safe_constantize || class_name.safe_constantize || name.safe_constantize
7
7
  end
8
8
 
9
9
  def datatable_klass
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-24 00:00:00.000000000 Z
11
+ date: 2017-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails