rows_controller 2.0.8 → 2.1.0

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: 2c1a2d58050cec8273532a04b600e01a0660d19b
4
- data.tar.gz: d6fb8fa93464f760c36b88b5234346b016f37e64
3
+ metadata.gz: 308a225c28bf1ea9ad3fdd9c2ab5da6a84432574
4
+ data.tar.gz: 92655caa6f8cac627066921b8845052c12faea9d
5
5
  SHA512:
6
- metadata.gz: 123dc690a1d8a8d827a5e82a44569e3964889f3b6bee90c2363bfbdf1eb4f76480e31fe5a3556145cb9166075a4a55b19b5a2b75363c1eb38fd5f4c7c819d5cc
7
- data.tar.gz: 918ebe33e38ea1b4b5abb69de8400e873e4351739788437f0cc2b57b252e6bbb1b4c2fac83ac02919485337fb2dd4383b332c2429b3e69a3d68c77e801c07097
6
+ metadata.gz: 15bd3f3928c8ac9af4114d35d46b190af2278ed72292e23817c272574c5b69bff123ba118e106e50702815fabf5664017b970be66a5e0aeaeb1e6fd95b78aaf8
7
+ data.tar.gz: 71b004b2703c3010b5d5ccbbfcec489275d76ac4fa1d4ed8d7c623141afcde5f846edae450472e762ee91bd0fbfe9bde73c42f52811f305f58098d6e6450bc96
data/README.md CHANGED
@@ -70,6 +70,12 @@ be changed by e.g.:
70
70
  The model class can be retrieved with the helper model_class.
71
71
 
72
72
 
73
+ Rails 5
74
+ -------
75
+
76
+ This gem is intended for Rails 5.
77
+ Older Rails versions may use "gem 'rows_controller', '= 2.0.8'".
78
+
73
79
  Rails 4
74
80
  -------
75
81
 
@@ -13,6 +13,7 @@ class RowsController < ApplicationController
13
13
  include Rows::Utils
14
14
 
15
15
  def self.model_class(model_class = nil)
16
+ @_model_class ||= nil
16
17
  @_model_class = model_class unless model_class.nil?
17
18
  @_model_class
18
19
  end
@@ -0,0 +1,88 @@
1
+ require 'rows/resources'
2
+ require 'rows/model'
3
+ require 'rows/utils'
4
+
5
+ class RowsController < ApplicationController
6
+ helper_method :set_resource, :set_resources, :resource, :resources
7
+ helper_method :resource_columns, :resource_format
8
+ helper_method :model_class, :model_name
9
+ helper_method :model_symbol, :model_symbol_plural
10
+
11
+ include Rows::Model
12
+ include Rows::Resources
13
+ include Rows::Utils
14
+
15
+ def self.model_class(model_class = nil)
16
+ @_model_class ||= nil
17
+ @_model_class = model_class unless model_class.nil?
18
+ @_model_class
19
+ end
20
+
21
+ # GET /:resources[.json]
22
+ def index
23
+ set_resources
24
+ end
25
+
26
+ # GET /:resource/:id[.json]
27
+ def show
28
+ set_resource
29
+ end
30
+
31
+ # GET /:resource/new
32
+ def new
33
+ resource_new
34
+ end
35
+
36
+ # GET /:resource/:id/edit
37
+ def edit
38
+ set_resource
39
+ end
40
+
41
+ # POST /:resources[.json]
42
+ def create
43
+ create_update(:resource_create, 'created')
44
+ end
45
+
46
+ # PATCH/PUT /:resources/:id[.json]
47
+ def update
48
+ set_resource
49
+ create_update(:resource_update, 'updated')
50
+ end
51
+
52
+ # DELETE /:resources/:id[.json]
53
+ def destroy
54
+ set_resource
55
+ resource_destroy
56
+ flash[:notice] = t('ui.destroyed', model: model_name).html_safe unless request.xhr?
57
+ respond_to do |format|
58
+ format.html { redirect_to action: :index }
59
+ format.js { render template: 'rows/destroy', layout: false }
60
+ format.json { head :no_content }
61
+ end
62
+ end
63
+
64
+ private
65
+ def create_update(which, msg)
66
+ respond_to do |format|
67
+ if self.send(which)
68
+ format.html {
69
+ flash[:notice] = t(msg, scope: :ui, model: model_name,
70
+ default: "%{model} was successfully #{msg}.").html_safe
71
+ if params[:commit] == 'OK'
72
+ redirect_to action: :index
73
+ else
74
+ redirect_to action: 'edit', id: resource.id
75
+ end
76
+ }
77
+ format.json { render action: 'show',
78
+ status: msg == 'created' ? :created : :ok,
79
+ location: resource }
80
+ else ## failed
81
+ format.html { render action: msg == 'created' ? :new : :edit }
82
+ format.json { render json: resource.errors,
83
+ status: :unprocessable_entity }
84
+ end
85
+ end
86
+ end
87
+
88
+ end
@@ -13,11 +13,11 @@ module Rows::Resources
13
13
  end
14
14
 
15
15
  def resources
16
- @rows || set_resources
16
+ @rows ||= set_resources
17
17
  end
18
18
 
19
19
  def resource
20
- @row || set_resource
20
+ @row ||= set_resource
21
21
  end
22
22
 
23
23
  private
@@ -38,6 +38,7 @@ module Rows::Resources
38
38
 
39
39
  def resource_update
40
40
  if Rails::VERSION::MAJOR >= 4
41
+ # return true unless params[model_symbol]
41
42
  resource.update(resource_params)
42
43
  else
43
44
  resource.update_attributes(resource_params)
@@ -61,7 +62,7 @@ module Rows::Resources
61
62
  # Never trust parameters from the scary internet, only allow the white list through.
62
63
  def resource_params
63
64
  permits = resource_whitelist
64
- if Rails::VERSION::MAJOR > 3
65
+ if params.respond_to?(:require)
65
66
  params.require(model_symbol).permit(permits)
66
67
  else
67
68
  pars = params[model_symbol] || {}
@@ -0,0 +1,80 @@
1
+ module Rows::Resources
2
+
3
+ def set_resources(rows = nil)
4
+ rows ||= model_class.all
5
+ instance_variable_set("@#{model_symbol_plural}", rows)
6
+ @rows = rows
7
+ end
8
+
9
+ def set_resource(row = nil)
10
+ row ||= model_class.find_by_id(params[:id].to_i)
11
+ instance_variable_set("@#{model_symbol}", row)
12
+ @row = row
13
+ end
14
+
15
+ def resources
16
+ @rows ||= set_resources
17
+ end
18
+
19
+ def resource
20
+ @row ||= set_resource
21
+ end
22
+
23
+ private
24
+ # low level resource methods
25
+ # can be monkey patched
26
+ def resource_new
27
+ if params[model_symbol]
28
+ set_resource model_class.new(resource_params)
29
+ else
30
+ set_resource model_class.new
31
+ end
32
+ end
33
+
34
+ def resource_create
35
+ resource_new
36
+ resource.save
37
+ end
38
+
39
+ def resource_update
40
+ if Rails::VERSION::MAJOR >= 4
41
+ return true unless params[model_symbol]
42
+ resource.update(resource_params)
43
+ else
44
+ resource.update_attributes(resource_params)
45
+ end
46
+ end
47
+
48
+ def resource_destroy
49
+ resource.destroy
50
+ end
51
+
52
+ def resource_columns
53
+ return model_class.column_headers if model_class.respond_to?(:column_headers)
54
+ return ['to_s'] unless model_class.respond_to?(:content_columns)
55
+ ['id'] + model_class.content_columns.collect{|c| c.name }
56
+ end
57
+
58
+ def resource_whitelist
59
+ raise "RowsController requires private method 'resource_whitelist' in controller <#{params[:controller]}>"
60
+ end
61
+
62
+ # Never trust parameters from the scary internet, only allow the white list through.
63
+ def resource_params
64
+ permits = resource_whitelist
65
+ if params.respond_to?(:require)
66
+ params.require(model_symbol).permit(permits)
67
+ else
68
+ pars = params[model_symbol] || {}
69
+ pars.keys.each { |x|
70
+ x = x.to_sym
71
+ unless permits.include?(x) || permits.include?({x => []})
72
+ pars.delete(x)
73
+ p "** WARNING: model <#{model_name}> dropping params <#{x}>"
74
+ end
75
+ }
76
+ pars
77
+ end
78
+ end
79
+
80
+ end
data/lib/rows/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rows
2
- VERSION = "2.0.8"
2
+ VERSION = '2.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rows_controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.8
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-08 00:00:00.000000000 Z
11
+ date: 2017-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -35,6 +35,7 @@ files:
35
35
  - README.md
36
36
  - Rakefile
37
37
  - app/controllers/rows_controller.rb
38
+ - app/controllers/rows_controller.rb.bak
38
39
  - app/controllers/rows_ext_controller.rb
39
40
  - app/views/rows/_form.slim
40
41
  - app/views/rows/_list.slim
@@ -56,10 +57,9 @@ files:
56
57
  - lib/rows/locales/en.yml
57
58
  - lib/rows/model.rb
58
59
  - lib/rows/resources.rb
60
+ - lib/rows/resources.rb.bak
59
61
  - lib/rows/utils.rb
60
- - lib/rows/utils.rb.bak
61
62
  - lib/rows/version.rb
62
- - lib/rows/version.rb.bak
63
63
  - lib/rows_controller.rb
64
64
  homepage: https://github.com/matique/rows_controller
65
65
  licenses:
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.5.1
84
+ rubygems_version: 2.6.12
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: RowsController DRYs your controllers.
@@ -1,19 +0,0 @@
1
- module Rows::Utils
2
-
3
- # formatting
4
- def resource_format(x)
5
- return '--'.html_safe if x.nil?
6
- bool = x.class == Time || x.class == Date || x.class == DateTime ||
7
- x.class == ActiveSupport::TimeWithZone
8
- return x.strftime('%d.%m.%Y').html_safe if bool
9
- # return I18n.l(x) if bool
10
- return x.to_s.html_safe if x.class == Fixnum
11
- return 'X'.html_safe if x.class == TrueClass
12
- return '&ensp;'.html_safe if x.class == FalseClass
13
- return x.call if x.class == Proc
14
- return '---'.html_safe if x.empty?
15
- str = x.to_s
16
- return str.gsub(/\r*\n/, '<br/>')
17
- end
18
-
19
- end
@@ -1,3 +0,0 @@
1
- module Rows
2
- VERSION = "2.0.7"
3
- end