crudable 0.1.0 → 0.2.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: 636f7f7d2e6c0d943fa0c1d0c3b71cef97a40d58
4
- data.tar.gz: c6f002bedf09a70abc8fbedc0bf6d9bdaed8e8f4
3
+ metadata.gz: 2fa8a13e56c480ec1132098764288a50978cc168
4
+ data.tar.gz: 0b1af86e27c0c15fddcd6c62f45cb2fea707fea9
5
5
  SHA512:
6
- metadata.gz: 4c09bbeb8d793347adb3b502ddeae85d1c85c07a6e38e09aa4abefc9e7dc95a3894053faa962edb302385d82ca78462f5c051aab4ec604cbe32d6356f75392a8
7
- data.tar.gz: fad934c345c63294cb4786244f368d7c7857c281d6e557de991e535cc766ecc7bc7c966be1a287bcf2ed7ec45d0161bd4c1469bb978d311980dba0a8a5385342
6
+ metadata.gz: f6f92771869881f865e6147509fe56374384a0fdcecf6f0a9e9be569c65e0f31b2433364ef2e38f51e1aa603c227cce238187e7de161267a81c6875607a84b85
7
+ data.tar.gz: bc901396a87d1c36ebf23bf2bba39c3c8f037c3ab4fa8c1d466573b1be06a328f00d84056104e4649932f7e571aed3714f4a6893d0f460f265957a72010bd1d6
data/README.md CHANGED
@@ -22,6 +22,11 @@ Or install it yourself as:
22
22
 
23
23
  TODO: Write usage instructions here
24
24
 
25
+ For now just use in your controller:
26
+ ```ruby
27
+ include CRUDable::Controller
28
+ ```
29
+
25
30
  ## Development
26
31
 
27
32
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/crudable.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "crudable/version"
2
+ require "crudable/controller"
2
3
 
3
4
  module CRUDable
4
5
  # Your code goes here...
@@ -0,0 +1,126 @@
1
+ require 'active_support/concern'
2
+
3
+ module CRUDable
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def index
9
+ instance_variable_set("@#{klass.table_name}", klass.all)
10
+
11
+ respond_to do |format|
12
+ format.html
13
+ format.json { render json: instance_variable_get("@#{klass.table_name}") }
14
+ format.xml { render xml: instance_variable_get("@#{klass.table_name}") }
15
+ end
16
+ end
17
+
18
+ def show
19
+ instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))
20
+
21
+ respond_to do |format|
22
+ format.html
23
+ format.json { render json: instance_variable_get("@#{klass_singular_variable}") }
24
+ format.xml { render xml: instance_variable_get("@#{klass_singular_variable}") }
25
+ end
26
+ end
27
+
28
+ def new
29
+ instance_variable_set("@#{klass_singular_variable}", klass.new)
30
+
31
+ respond_to do |format|
32
+ format.html { render 'form' }
33
+ format.json { render json: instance_variable_get("@#{klass_singular_variable}") }
34
+ format.xml { render xml: instance_variable_get("@#{klass_singular_variable}") }
35
+ end
36
+ end
37
+
38
+ def create
39
+ instance_variable_set("@#{klass_singular_variable}", klass.new(send("#{klass_singular_variable}_params")))
40
+
41
+ respond_to do |format|
42
+ if instance_variable_get("@#{klass_singular_variable}").save
43
+ format.html { redirect_to({ action: :index }) }
44
+ format.json { render json: instance_variable_get("@#{klass_singular_variable}"), status: :created, location: instance_variable_get("@#{klass_singular_variable}") }
45
+ format.xml { render xml: instance_variable_get("@#{klass_singular_variable}"), status: :created, location: instance_variable_get("@#{klass_singular_variable}") }
46
+ else
47
+ format.html { render 'form' }
48
+ format.json { render json: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
49
+ format.json { render xml: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
50
+ end
51
+ end
52
+ end
53
+
54
+ def edit
55
+ instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))
56
+
57
+ respond_to do |format|
58
+ format.html { render 'form' }
59
+ format.json { render json: instance_variable_get("@#{klass_singular_variable}") }
60
+ format.xml { render xml: instance_variable_get("@#{klass_singular_variable}") }
61
+ end
62
+ end
63
+
64
+ def update
65
+ instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))
66
+
67
+ respond_to do |format|
68
+ if instance_variable_get("@#{klass_singular_variable}").update(send("#{klass_singular_variable}_params"))
69
+ format.html { redirect_to({ action: :index }) }
70
+ format.json { head :ok }
71
+ format.xml { head :ok }
72
+ else
73
+ format.html { render 'form' }
74
+ format.json { render json: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
75
+ format.json { render xml: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
76
+ end
77
+ end
78
+ end
79
+
80
+ def destroy
81
+ instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))
82
+
83
+ if instance_variable_get("@#{klass_singular_variable}").destroy
84
+ flash[:notice] = 'Successfully deleted.'
85
+ else
86
+ flash[:error] = 'Delete failed.'
87
+ end
88
+
89
+ respond_to do |format|
90
+ if instance_variable_get("@#{klass_singular_variable}").destroy
91
+ format.html { redirect_to(:back) }
92
+ format.json { head :ok }
93
+ format.xml { head :ok }
94
+ else
95
+
96
+ end
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def klass
103
+ controller_name.classify.constantize
104
+ end
105
+
106
+ def klass_singular_variable
107
+ klass.to_s.downcase.freeze
108
+ end
109
+
110
+ define_method "#{klass_singular_variable}_params" do
111
+ logger.warn "TODO: Please configure this in included controller."
112
+ params.require("#{klass_singular_variable}".to_sym).permit!
113
+ end
114
+ end
115
+
116
+ class_methods do
117
+ def klass
118
+ controller_name.classify.constantize
119
+ end
120
+
121
+ def klass_singular_variable
122
+ klass.to_s.downcase.freeze
123
+ end
124
+ end
125
+ end
126
+ end
@@ -1,3 +1,3 @@
1
1
  module CRUDable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crudable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vjatseslav Gedrovits
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-01 00:00:00.000000000 Z
11
+ date: 2016-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - bin/setup
71
71
  - crudable.gemspec
72
72
  - lib/crudable.rb
73
+ - lib/crudable/controller.rb
73
74
  - lib/crudable/version.rb
74
75
  homepage: https://github.com/Gedrovits/crudable
75
76
  licenses: