resourcify 0.0.2 → 0.0.3

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: 35ae2a1538823e8d0c2c5b48bed067a61c7617e1
4
- data.tar.gz: 41ef92c2d410713392e2c1654a1f0b345ae5609c
3
+ metadata.gz: ce0387e71b16f415b6e60fdc2c6a8c87cf619ce7
4
+ data.tar.gz: 7edfd0fa2a98bb1586459c98255774f1004f957a
5
5
  SHA512:
6
- metadata.gz: 9f5d18268a7af685c16b8acdb6a527a12c3cb65aaf6781564cef44e807c5f516321c41591f1d7e0a5ba1a67745eb5ee363247b8739013fb899035adf0e473ee2
7
- data.tar.gz: c882e3506a2f7920bdd1b7edd3bfeee72c0cf6358ed2293c893591a7f2bdf7651845e29775a538b9e716e4368bd1a8c77cfa06329add29636e4106fb3b1bc848
6
+ metadata.gz: 8eeb99efad71fcae6e9ad3cb7197bcce6e1ffa77a4cffb4afff4a590ed1e0c9968e49ebf9d5364300fe55f648bce314c0fff570e93b1890203864639f5be2252
7
+ data.tar.gz: efd021a1a002a47dab329669025317f69c8566ed85c221cecbf789ccdf54f7927ae5310c10624556bda7584b5144308d9f922c386626d61a92a155302fa12956
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Resourcify
1
+ # Resourcify [![Gem Version](https://badge.fury.io/rb/resourcify.png)](http://badge.fury.io/rb/resourcify)
2
2
 
3
3
  Resourcify is a rails gem that helps to speed up development by giving you json api controllers that inherit all restful actions. It also makes your models easier to filter by adding a "resourcify_filter" method. This gem behaves as an "acts_as" gem by using ActiveSupport Concerns.
4
4
 
@@ -3,7 +3,9 @@ module Controller::Actions
3
3
  def index
4
4
  authorize _RC.new
5
5
 
6
- recs = policy_scope(_RC.all)
6
+ # recs = policy_scope(_RC.all)
7
+ recs = policy_scope(_RC.includes(belongs_tos))
8
+ # recs = policy_scope(_RC.includes([]).all)
7
9
 
8
10
  # apply resourcify_filter if present and query param is also present
9
11
  if recs.respond_to? "resourcify_filter" and params[:query].present?
@@ -52,7 +52,11 @@ module Controller
52
52
 
53
53
  # Use callbacks to share common setup or constraints between actions.
54
54
  def set_record
55
- @record = _RC.find(params[:id])
55
+ @record = _RC.includes(belongs_tos).find(params[:id])
56
+ end
57
+
58
+ def belongs_tos
59
+ _RC.reflect_on_all_associations(:belongs_to).map {|e| e.name }
56
60
  end
57
61
 
58
62
  # Only allow a trusted parameter "white list" through.
@@ -0,0 +1,110 @@
1
+ module Model
2
+ module Tpl
3
+ class ResourcifyTpl < Struct.new(:model_class)
4
+
5
+ def columns
6
+ fields = []
7
+ foreign_keys = self.model_class.reflections.each_with_object({}) {|(k, v), h| h[v.foreign_key] = v.name.to_s }
8
+
9
+ self.model_class.columns.each do |c|
10
+ f = { name: c.name, type: c.type.to_s, label: c.name.titleize }
11
+ if foreign_keys.keys.include?(c.name)
12
+ f[:lookup] = foreign_keys[c.name]
13
+ f[:label] = c.name[0, c.name.length - 3].titleize if c.name.ends_with?("_id")
14
+ if foreign_keys[c.name] == 'children'
15
+ f[:lookup] = :parent
16
+ f[:label] = "Parent #{self.model_class.name.split('::').last.singularize.titleize}"
17
+ end
18
+ end
19
+ fields.push f
20
+ end
21
+
22
+ fields
23
+ rescue Exception => e
24
+ []
25
+ end
26
+
27
+ def lookups
28
+ if _tpl = method_exists?('lookups')
29
+ return _tpl.new.lookups
30
+ end
31
+
32
+ lookups = {}
33
+ model_class.reflect_on_all_associations(:belongs_to).each do |association|
34
+ lookups[association.name] = association.klass.all.map { |e| {id: e.id, name: e.name} }
35
+ lookups[association.name].unshift({ id: nil, name: '' })
36
+
37
+ # if association.name == :parent
38
+ # lookups[association.name] = association.klass.all
39
+ # lookups[association.name].unshift(association.klass.new(id: nil, name: 'N/A'))
40
+ # else
41
+ # lookups[association.name] = association.klass.all.map { |e| {id: e.id, name: e.name} }
42
+ # end
43
+ end
44
+
45
+ lookups
46
+ rescue Exception => e
47
+ {}
48
+ end
49
+
50
+ def form_columns
51
+ if _tpl = method_exists?('form_columns')
52
+ return _tpl.new.form_columns
53
+ end
54
+
55
+ excluded_fields = ["id", "created_at", "updated_at", "lft", "rgt", "depth"]
56
+ fields = self.columns.select { |e| !excluded_fields.include? e[:name] }
57
+
58
+ rescue Exception => e
59
+ []
60
+ end
61
+
62
+ def grid_columns
63
+ if _tpl = method_exists?('grid_columns')
64
+ return _tpl.new.grid_columns
65
+ end
66
+
67
+ excluded_fields = ["id", "created_at", "updated_at", "lft", "rgt", "depth"]
68
+ fields = self.columns.select { |e| !excluded_fields.include? e[:name] }
69
+
70
+ rescue Exception => e
71
+ []
72
+ end
73
+
74
+ def options
75
+ if _tpl = method_exists?('options')
76
+ return _tpl.new.options
77
+ end
78
+ {}
79
+ rescue
80
+ {}
81
+ end
82
+
83
+ def filters
84
+ if _tpl = method_exists?('filters')
85
+ return _tpl.new.filters
86
+ end
87
+ {}
88
+ rescue
89
+ {}
90
+ end
91
+
92
+ private
93
+ def method_exists?(method)
94
+ _tpl = "#{model_class.name}Tpl".constantize
95
+
96
+ if _tpl.new.respond_to?(method)
97
+ _tpl
98
+ else
99
+ false
100
+ end
101
+ rescue
102
+ false
103
+ end
104
+ end
105
+
106
+ def tpl
107
+ ResourcifyTpl.new(self)
108
+ end
109
+ end
110
+ end
@@ -1,5 +1,6 @@
1
1
  require "resourcify/model/resourcify_filter"
2
2
  require "resourcify/model/policy_class"
3
+ require "resourcify/model/tpl"
3
4
  require "resourcify/controller/base"
4
5
  require "resourcify/controller/actions/index"
5
6
  require "resourcify/controller/actions/create"
@@ -27,6 +28,9 @@ module Resourcify
27
28
  # Add policy_class method for pundit
28
29
  send :extend, Model::PolicyClass
29
30
 
31
+ # Add tpl methods
32
+ send :extend, Model::Tpl
33
+
30
34
  # Include instance methods
31
35
  send :include, ModelInstanceMethods
32
36
  elsif self.ancestors.include?(ActionController::Base) # controllers
@@ -1,3 +1,3 @@
1
1
  module Resourcify
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resourcify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Baidu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-14 00:00:00.000000000 Z
11
+ date: 2014-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_model_serializers
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: pundit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.2
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: sqlite3
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -38,7 +66,7 @@ dependencies:
38
66
  - - '>='
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
41
- description: Resourcify gem
69
+ description: Resoucify rails controllers and models
42
70
  email:
43
71
  - stephen@axoninfosystems.com
44
72
  executables: []
@@ -57,6 +85,7 @@ files:
57
85
  - lib/resourcify/controller/base.rb
58
86
  - lib/resourcify/model/policy_class.rb
59
87
  - lib/resourcify/model/resourcify_filter.rb
88
+ - lib/resourcify/model/tpl.rb
60
89
  - lib/resourcify/resourcify.rb
61
90
  - lib/resourcify/version.rb
62
91
  - lib/tasks/resourcify_tasks.rake
@@ -130,7 +159,7 @@ rubyforge_project:
130
159
  rubygems_version: 2.2.2
131
160
  signing_key:
132
161
  specification_version: 4
133
- summary: Resourcify gem
162
+ summary: Resoucify rails controllers and models
134
163
  test_files:
135
164
  - test/acts_as_resourcify_test.rb
136
165
  - test/dummy/app/assets/javascripts/application.js