rails_codebook 0.0.1.beta → 0.0.1

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: 6d02ea772bbf52b8be1cb22f6ffbcd0a86a8779b
4
- data.tar.gz: 54ef973c07627d66557c920ce8c69023a44ef0fe
3
+ metadata.gz: b4ca683bbccb048e140d63deeb150c340a3f315b
4
+ data.tar.gz: e991f73a6ca356531ddbeb1b450e1113e0a8b602
5
5
  SHA512:
6
- metadata.gz: 35e0389c0c63ae021510c913f640a53be102444fea94a85570513e8e2e1a5b9425817472cddb268c330e7e929d9f36f6cc5b9d5240d539b4a1fde44f822519a8
7
- data.tar.gz: 0997f112a561240c04a3eb4e5b8875c86c55f72f5250e2e56c181e6685fcf7d7148e5b1046d0e7b1d56fc57afe13470dcd3a720aa0ed89a472dff4624becc161
6
+ metadata.gz: 0b6fb66ecc528dd99d26031edd162a1030e371a0af29007ce08f894620cad3a3f6a81b6aace626ca991946a1616fdc39695bf29d37075c3965fa3c5b5128a51d
7
+ data.tar.gz: f33c3b614f77b7deecda9c39582ec54794823c9946e378e6b5e2002f13ad4873cb998c8c67514183b8c4fb11c946355aca92b9bfebb2f247687990c621e7bde9
data/README.md CHANGED
@@ -1,5 +1,233 @@
1
- # WORK in PROGRESS
1
+ # RailsCodebook
2
+
3
+ ### Bear in mind, this is still WORKinPROGRESS and can be buggy, all issues filed will be appreciated as well as new ideas and help of all kinds :)
2
4
 
3
5
  [![Build Status](https://travis-ci.org/redrick/rails_codebook.png?branch=master)](https://travis-ci.org/redrick/rails_codebook)
4
6
  [![Coverage Status](https://coveralls.io/repos/redrick/rails_codebook/badge.png)](https://coveralls.io/r/redrick/rails_codebook)
5
- [![Gem Version](https://badge.fury.io/rb/rails_codebook.png)](http://badge.fury.io/rb/rails_codebook)
7
+ [![Gem Version](https://badge.fury.io/rb/rails_codebook.png)](http://badge.fury.io/rb/rails_codebook)
8
+
9
+ ## What is this ?
10
+
11
+ This is a gem tested with ruby 2.0.0-p247 and rails 4.0.0
12
+ First of all what the codebook is... usually is the collection of values that can be passed to [select_tag](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag). As we are working on bigger projects, we do not want to make them heavy by just adding other model and controller and bunch of stuff for every codebook we use, there are potentially ten, almost a hundred in each project and it started to be overwhelming.
13
+ So there we go and create this little thing, which stores all the values for us in [redis](http://redis.io/) (noSQL database) and lets us comunicate with all the values through simple DSL, I will explain all as you read :)
14
+ OK that is the intro, lets dive in... (oh and for those "pics or didn't happen")
15
+
16
+ ![#](https://raw.github.com/redrick/rails_codebook/master/doc/pictures/select_box.png)
17
+
18
+ ## How can I install the damn thing ?
19
+
20
+ simple as always:
21
+ ``` ruby
22
+ gemm install rails_codebook
23
+ ```
24
+
25
+ or by adding to your gemfile:
26
+ ```
27
+ gem 'rails_codebook'
28
+ ```
29
+
30
+ and running:
31
+ ```
32
+ bundle install
33
+ ```
34
+
35
+ after bundle, there is just the simple step of mounting routes like this (this goes to your ```config/routes.rb```):
36
+ ```
37
+ # mount with
38
+ mount RailsCodebook::Engine => '/codebooks'
39
+ ```
40
+
41
+
42
+ ## How is it used ?
43
+
44
+ After installing gem make sure you redis is running, if not simply run it with:
45
+ ```
46
+ redis-server
47
+ ```
48
+
49
+ ### Configuration
50
+
51
+ Configuration is pretty simple for now, you just need to set the redis connection,
52
+ if not the default one will be invoked (```Redis.new```)
53
+ otherwise set:
54
+ ```
55
+ RailsCodebook.redis = Redis.new() # I use it with a db number :)
56
+ ```
57
+
58
+ ### Possibilities
59
+
60
+ #### 2WAYS
61
+
62
+ So as wee see it there are two options you want to put inside the select boxes:
63
+ = first are the values specially created and only used in main model in form of FK that references to this values
64
+ = and the second are the activerecord models that need to be codebookified, so they are not that heavy and can be worked with
65
+
66
+ so I got two solutions:
67
+ = has_codebooks
68
+ = acts_as_codebook
69
+ (will explain the usage later)
70
+
71
+ #### GUI
72
+
73
+ As I was doing this, I needed some interface to manage Redis values apart from [redis-commander](http://nearinfinity.github.io/redis-commander/) which can be messy when I am only interested in some stuff that I want to add/remove/edit...
74
+ So here are screen of the GUI made for the values so we got them under controll :)
75
+
76
+
77
+ ![#](https://raw.github.com/redrick/rails_codebook/master/doc/pictures/screen-1.png)
78
+
79
+ ![#](https://raw.github.com/redrick/rails_codebook/master/doc/pictures/screen-2.png)
80
+
81
+ ![#](https://raw.github.com/redrick/rails_codebook/master/doc/pictures/screen-3.png)
82
+
83
+ ![#](https://raw.github.com/redrick/rails_codebook/master/doc/pictures/screen-4.png)
84
+
85
+ #### API
86
+
87
+ Also I am using it in Rails project that is REST API (as server) and there is Angularjs front, which from time to time needs (on search pages etc..) to ask for one/all members of certain codebook, so there is the light API:
88
+
89
+ as you added the ```mount RailsCodebook::Engine => '/codebooks'``` into your routes, whole gem is accessible at that route, I will take it as root (it actually is root for the gem :) )
90
+
91
+ you can use routes:
92
+
93
+ # list of all codebook values
94
+ # you can change the language (because I forgot to mention that
95
+ # I count with you using 'name' column as place for translation key)
96
+ # use only two letters {en, cs, sk, pl}
97
+ ```GET "/api/(:lang)"```
98
+
99
+ # you can look through all values by passing GET query under 'q' key
100
+ ```GET "/api/(:lang)?q=somequery"```
101
+
102
+ # for getting one codebook, cb_name is its name, please do not use others that '_' in names
103
+ ```GET "/(:lang)/:cb_name"```
104
+
105
+ # you can look through the codebook values by passing GET query under 'q' key
106
+ ```GET "/(:lang)/:cb_name?q=somequery"```
107
+
108
+ # show method of one of the codebooks thorugh its cb_name and id
109
+ ```GET "/(:lang)/:cb_name/:id"```
110
+
111
+ ### Usage
112
+
113
+ OK and now to the best part :) I got this developed and tested against ActiveRecord '4.0.1' but I am not saying it is not possible it will run on older versions too...
114
+ It adds two methods to Activerecord that generate those codebook ones, here they are both in one model:
115
+ ``` ruby
116
+ class Article < ActiveRecord::Base
117
+
118
+ has_codebooks({
119
+ 'importance_cb' => 'importances',
120
+ 'options' => {
121
+ 'fk_cb' => 'value'
122
+ }
123
+ })
124
+
125
+ acts_as_codebook({
126
+ 'key' => 'id',
127
+ 'value' => 'title'
128
+ })
129
+ end
130
+ ```
131
+ #### has_codebooks
132
+
133
+ this method is for defining which codebooks is the model using and for better usability olson on which column which codebook is mapped, so the use for simple declaring the column => codebook :
134
+
135
+ ```ruby
136
+ has_codebooks({
137
+ 'importance_cb' => 'importances'
138
+ })
139
+ ```
140
+
141
+ you can pass as many codebooks as you want and as added value I was solving polymorphism, when you got a model like [HERE](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations) and I came to issue, that every model that can be associated with the polymorphic table needs to have its role in the polymorphic table (given that every model has set of its own roles)... this started to be messy, so I added it here and you can use it like this:
142
+ ```ruby
143
+ has_codebooks({
144
+ "role_cb" => "polymorphic" # says look to options, you are not going to find cb_name here :)
145
+ "options" => {
146
+ "role_cb" => { # in case there are more codebooks that need these options
147
+ "cb_format" => "projectable_type" # or will always be joined from :
148
+ # cb_format+"_"+model.table_name+"_"+column_name.pluralized
149
+ }
150
+ }
151
+ })
152
+ ```
153
+
154
+ Also for the curious cases I added the 'fk_cb' config option, it is for defining what will be put as FK (yes sometimes you need a string value otherwise it is always the ID), you can use it inside the column config or inside options alone(for all):
155
+ ```ruby
156
+ has_codebooks({
157
+ "status_cb" => "boolean", # defining column_name => codebook_name
158
+ "importance_cb" => "importance",
159
+ "options" => {
160
+ "status_cb" => {
161
+ "fk_cb" => "id" # which value from codebook to use as FK for the model (usually id, value)
162
+ # can also be defined for each one codebook :)
163
+ }
164
+ "fk_cb" => "value" # all other will use this fallback or the default _value one :)
165
+ }
166
+ })
167
+ ```
168
+ If you need more, I will gladly add those, just write a suggestion in issues :)
169
+
170
+
171
+ Calling this in your model adds a bunch of methods into the ActiveRecord model always starting in the column_name stripped from '_cb' so for 'type_cb' all will start with 'type' (now on I will use base_column_name):
172
+
173
+ * base_method_name+"_object" => gives you the actual rel object
174
+
175
+ * self.base_method_name+"_codebook_name" => prints out the codebook name for you
176
+
177
+ * base_method_name => serialized json format of actual object
178
+
179
+ * self.base_method_name+"_object_all" => returns array of all codebook objects
180
+
181
+ * self.base_method_name+"_collection" => collection that can be passed to select_tag options
182
+
183
+ * self.base_method_name+"_all" => serialized object_all
184
+
185
+ * self.base_method_name+"_values" => array of all "values" (translated names into actual language)
186
+
187
+ * self.base_method_name+"_keys" => array of all keys
188
+
189
+
190
+ #### acts_as_codebook
191
+
192
+ is the second way I already mentioned, you can easily transform existing ActiveRecord model into codebook by adding this to it and maybe defining which value to take as key and which as value, like this:
193
+ ```ruby
194
+ acts_as_codebook({
195
+ "key" => "name_for_key",
196
+ "value" => "name_for_value"
197
+ })
198
+ ```
199
+ it, as previous one adds some methods to ActiveRecord model, so if you got problems with method names, I gave you the 'method' option to set different name, otherwise the default (model_name) will be appplied:
200
+ ```ruby
201
+ acts_as_codebook({
202
+ "method" => "method_base_name"
203
+ })
204
+ ```
205
+ And here are methods that are added (method name will be reffered to as base_method_name as before :) ):
206
+
207
+ * base_method_name+"_object" => gives you the actual object in cb form
208
+
209
+ * self.base_method_name+"_name" => prints out the codebook name for you
210
+
211
+ * base_method_name+"_this" => serialized json format of actual object
212
+
213
+ * self.base_method_name+"_object_all" => returns array of all model rows as cb objects
214
+
215
+ * self.base_method_name+"_all" => serialized object_all
216
+
217
+ * self.base_method_name+"_values" => array of all "values"
218
+
219
+ * self.base_method_name+"_keys" => array of all keys
220
+
221
+
222
+ #### OK that is all for now, TUNE FOR MORE INFO
223
+
224
+ ## Contributing
225
+
226
+ 1. Fork it
227
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
228
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
229
+ 4. Push to the branch (`git push origin my-new-feature`)
230
+ 5. Create new Pull Request
231
+
232
+ [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/redrick/rails_codebook/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
233
+
@@ -1,3 +1,3 @@
1
1
  module RailsCodebook
2
- VERSION = "0.0.1.beta"
2
+ VERSION = "0.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_codebook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - redrick
@@ -440,9 +440,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
440
440
  version: '0'
441
441
  required_rubygems_version: !ruby/object:Gem::Requirement
442
442
  requirements:
443
- - - '>'
443
+ - - '>='
444
444
  - !ruby/object:Gem::Version
445
- version: 1.3.1
445
+ version: '0'
446
446
  requirements: []
447
447
  rubyforge_project:
448
448
  rubygems_version: 2.1.5