baseapi 0.1.1 → 0.1.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: b07759ff04f4ef7076a6325f7e10e377d3725a55
4
- data.tar.gz: 5da5a0e31fb7ddf700b8f8590d1ad55dc13bb81b
3
+ metadata.gz: 562c675140df0d7fc675e570b5b984a3eff8249b
4
+ data.tar.gz: aad3e7568ca8c0b3bac6b554f285066a2744f469
5
5
  SHA512:
6
- metadata.gz: 35d5bdcab041a9b256a364009acd6c161a5b8fa410321e34ce786a5bb088df387b50cf0880ddf6f01ea670a3f98559314a4a60a0761cbe69af468abb05fc3780
7
- data.tar.gz: 94fa1f09dd989912a66267be23f3a510cd51878cba59e1ccc39df7239de99395b74a818ae4b10da78ebf6d32fade633b55fa00ddc5d2a300ef89bc66ed15d425
6
+ metadata.gz: 38a522250027c0d2f6e696f4e15fbdea713f09c79bedd89fca55fc3b18779eae95b56fa360e2d8542f6de251411176ad4376cba607a3d48e3607b4f76bc1cdb0
7
+ data.tar.gz: ad8bf3e76b7d185b446b8760714af39a7839f7e9bf7eee716091e356cd99b0bbaaf375fc2d00940c81e733bdb4eba37842cd3e06b503e7bc814ce92e41b5cd49
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Baseapi
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/baseapi`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ When you create a web application in the rails, If you want to CRUD operations in Ajax, might this gem is useful.
4
+ We only define the empty Controller and Model for us to define the CRUD in.
6
5
 
7
6
  ## Installation
8
7
 
@@ -22,7 +21,205 @@ Or install it yourself as:
22
21
 
23
22
  ## Usage
24
23
 
25
- TODO: Write usage instructions here
24
+ Introduction create a BaseApiController & JBuilder of view:
25
+
26
+ $ bundle exec baseapi setup
27
+
28
+ Create a Model (app/models/user.rb):
29
+
30
+ class User < ActiveRecord::Base
31
+ end
32
+
33
+ Extend the BaseApiController is when you create a Controller (app/controllers/users_controller.rb):
34
+
35
+ class CompaniesController < BaseApiController
36
+ end
37
+
38
+ Routing configuration (config/routes.rb):
39
+
40
+ constraints(:format => /json/) do
41
+ get "users" => "users#index"
42
+ get "users/:id" => "users#show"
43
+ post "users" => "users#create"
44
+ patch "users/:id" => "users#update"
45
+ put "users/:id" => "users#update"
46
+ delete "users/:id" => "users#destroy"
47
+ end
48
+
49
+ Corresponding API:
50
+
51
+ /users.json index GET
52
+ /users/{id}.json show GET
53
+ /users.json create POST
54
+ /users/{id}.json update PUT
55
+ /users/{id}.json destroy DELETE
56
+
57
+
58
+ ### Examples
59
+
60
+ Model
61
+
62
+ class User < ActiveRecord::Base
63
+ belongs_to :company
64
+ end
65
+
66
+ class Company < ActiveRecord::Base
67
+ has_many :users
68
+ end
69
+
70
+ Users table data
71
+
72
+ | id | name | company_id |
73
+ |----|----------|------------|
74
+ | 1 | arakawa | 1 |
75
+ | 2 | moriyuki | 2 |
76
+
77
+ Company table data
78
+
79
+ | id | name |
80
+ |----|----------|
81
+ | 1 | Google |
82
+ | 2 | Apple |
83
+
84
+ #### action index
85
+
86
+ Get all
87
+
88
+ GET /users.json
89
+
90
+ Specify the name
91
+
92
+ GET /users.json?name=arakawa
93
+
94
+ Specify multiple possible
95
+
96
+ GET /users.json?name[]=arakawa&name[]=moriyuki
97
+
98
+ Specify the belongs to company name
99
+
100
+ GET /users.json?company[name]=arakawa
101
+
102
+ Specify the has many users name
103
+
104
+ GET /companies.json?user[name]=hoge
105
+
106
+ #### action show
107
+
108
+ Get id 1
109
+
110
+ GET /users/1.json
111
+
112
+ #### action create
113
+
114
+ Create a user name is 'hoge'
115
+
116
+ POST /users.json?name=hoge
117
+
118
+ #### action update
119
+
120
+ Update the name to 'huga'
121
+
122
+ PATCH /users/1.json?name=huga
123
+ PUT /users/1.json?name=huga
124
+
125
+ #### action delete
126
+
127
+ Delete the id to 1
128
+
129
+ DELETE /users/1.json
130
+
131
+ #### return json format
132
+
133
+ {
134
+ error: false,
135
+ message: "",
136
+ data: [
137
+ {
138
+ id: 1,
139
+ name: "hoge"
140
+ },
141
+ {
142
+ id: 2,
143
+ name: "huga"
144
+ }
145
+ ]
146
+ }
147
+
148
+ ### Override
149
+
150
+ You can corresponding to the logical deletion, if you want to search condition to like and or, you will be able to override the processing in the Model
151
+
152
+
153
+ Get all
154
+
155
+ class User < ActiveRecord::Base
156
+ def self._all
157
+ end
158
+ end
159
+
160
+ delete
161
+
162
+ class User < ActiveRecord::Base
163
+ def _destroy
164
+ end
165
+ end
166
+
167
+ column search
168
+
169
+ class User < ActiveRecord::Base
170
+ def self._where(models, column, values)
171
+ end
172
+ end
173
+
174
+ name column search
175
+
176
+ class User < ActiveRecord::Base
177
+ def self._where_name(models, column, values)
178
+ end
179
+ end
180
+
181
+ belongs_to search
182
+
183
+ class User < ActiveRecord::Base
184
+ def self._belongs_to(models, table, hash)
185
+ end
186
+ end
187
+
188
+ company belongs_to search
189
+
190
+ class User < ActiveRecord::Base
191
+ def self._belongs_to_company(models, table, hash)
192
+ end
193
+ end
194
+
195
+ has_many search
196
+
197
+ class Company < ActiveRecord::Base
198
+ def self._has_many(models, table, hash)
199
+ end
200
+ end
201
+
202
+ users has_many search
203
+
204
+ class Company < ActiveRecord::Base
205
+ def self._has_many_users(models, table, hash)
206
+ end
207
+ end
208
+
209
+
210
+ There is a useful function for the associated table Search
211
+ By default, it looks like the following
212
+ 'Like' search and, you can change the 'and' and 'or'
213
+
214
+ class User < ActiveRecord::Base
215
+ def self._belongs_to(models, table, hash)
216
+ relation_match(models, table, hash, operator:'or') # default is match OR
217
+ # relation_like(models, table, hash, operator:'or') # LIKE OR
218
+ # relation_like(models, table, hash, operator:'and') # LIKE AND
219
+ end
220
+ end
221
+
222
+ The short so please read the [code](https://github.com/arakawamoriyuki/baseapi/blob/master/lib/baseapi/active_record/base_extension.rb) for more information
26
223
 
27
224
  ## Development
28
225
 
@@ -32,7 +229,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
229
 
33
230
  ## Contributing
34
231
 
35
- 1. Fork it ( https://github.com/[my-github-username]/baseapi/fork )
232
+ 1. Fork it ( https://github.com/arakawamoriyuki/baseapi/fork )
36
233
  2. Create your feature branch (`git checkout -b my-new-feature`)
37
234
  3. Commit your changes (`git commit -am 'Add some feature'`)
38
235
  4. Push to the branch (`git push origin my-new-feature`)
@@ -18,7 +18,7 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
18
18
  # @param String column column name
19
19
  # @param Array values search values
20
20
  def _where(models, column, values)
21
- model.where!(column => values)
21
+ models.where!(column => values)
22
22
  end
23
23
 
24
24
  # override or create method '_belongs_to_{table}' if necessary
@@ -1,3 +1,3 @@
1
1
  module Baseapi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baseapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moriyuki Arakawa