corn_starch 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/application_controller.rb +200 -0
  3. data/app/controllers/sessions_controller.rb +36 -0
  4. data/app/helpers/corn_starch/corn_starch_helper.rb +44 -4
  5. data/app/views/corn_starch/layouts/_notifications.html.erb +7 -0
  6. data/app/views/corn_starch/layouts/_pagination.html.erb +21 -0
  7. data/config/initializers/corn_starch_config.rb +33 -0
  8. data/config/locales/en.yml +58 -0
  9. data/config/locales/fr.yml +51 -0
  10. data/lib/corn_starch/version.rb +1 -1
  11. metadata +9 -18
  12. data/app/assets/fonts/corn_starch/glyphicons-halflings-regular.eot +0 -0
  13. data/app/assets/fonts/corn_starch/glyphicons-halflings-regular.svg +0 -288
  14. data/app/assets/fonts/corn_starch/glyphicons-halflings-regular.ttf +0 -0
  15. data/app/assets/fonts/corn_starch/glyphicons-halflings-regular.woff +0 -0
  16. data/app/assets/fonts/corn_starch/glyphicons-halflings-regular.woff2 +0 -0
  17. data/app/assets/javascripts/corn_starch/bootstrap.js +0 -2363
  18. data/app/assets/javascripts/corn_starch/bootstrap.min.js +0 -7
  19. data/app/assets/javascripts/corn_starch/npm.js +0 -13
  20. data/app/assets/stylesheets/corn_starch/bootstrap-theme.css +0 -587
  21. data/app/assets/stylesheets/corn_starch/bootstrap-theme.css.map +0 -1
  22. data/app/assets/stylesheets/corn_starch/bootstrap-theme.min.css +0 -6
  23. data/app/assets/stylesheets/corn_starch/bootstrap-theme.min.css.map +0 -1
  24. data/app/assets/stylesheets/corn_starch/bootstrap.css +0 -6760
  25. data/app/assets/stylesheets/corn_starch/bootstrap.css.map +0 -1
  26. data/app/assets/stylesheets/corn_starch/bootstrap.min.css +0 -6
  27. data/app/assets/stylesheets/corn_starch/bootstrap.min.css.map +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d4001684c8aec6790729b78f98fe2041f6cfa07
4
- data.tar.gz: 0929797b47e69c9b3a03f34faea799dc6712936f
3
+ metadata.gz: ae61e1aad76f9b55fd99ea16106e38398adb03c5
4
+ data.tar.gz: 2f7d494416ea0bfc2eb04610a2987c6824d620ff
5
5
  SHA512:
6
- metadata.gz: 208715b6f04eceecd229dd9193c02cc0eee07e288858b70c1d703528366924e77de2c5ba6ab549209198fb12ec89c07a3f437a7343313ea1ee72f8545701eeaa
7
- data.tar.gz: a68231b3c3efa8b9fe3df5bab910a7620d769ca53aec9c6d26e7f048821ce0c28aed1153f52ef73a28928cff020f07b1233568b9ba16dbc48e01e9e7ca4d77ab
6
+ metadata.gz: e6a2903646cd540dc3b1878987b13b0294309cab0aec362488d8d945087dd7911abf8a1d2b1752251ed71a2bf49e78465c7a9e39994c933fe4da411636ff98de
7
+ data.tar.gz: 6f613a83818a11c1513a68bcffbceee28d853b046bb32d59b47a9fec8215622b66e99b31e8db1261e66ef44aabb3247700d40fd45dd53b0462353bb47723e926
@@ -0,0 +1,200 @@
1
+ # CornStarch
2
+ # by Eresse <eresse@eresse.net>
3
+
4
+ # CornStarch Module
5
+ module CornStarch
6
+
7
+ # Application Controller
8
+ class ApplicationController < ActionController::Base
9
+
10
+ # CSRF Protection
11
+ protect_from_forgery with: :exception
12
+
13
+ # Global Filters
14
+ before_filter :pagination_filter
15
+
16
+ # Helper Methods
17
+ helper_method :user_model, :current_user, :is_authed, :is_self, :paginate, :notice_msg
18
+
19
+ ####################
20
+ # Controller Utils #
21
+ ####################
22
+
23
+ # Universal Search
24
+ def unisearch model, search
25
+
26
+ # Initialize Variables
27
+ search_model = model
28
+ search_q = ''
29
+ search_a = []
30
+ search_fields = {}
31
+
32
+ # Run through Model's Associations
33
+ model.searchable_associations.each do |a|
34
+
35
+ # Join into Search Model
36
+ search_model = search_model.includes a
37
+
38
+ # Acquire Association Target Model
39
+ assoc = model.reflect_on_association(a).klass
40
+
41
+ # Add Target Model's Search Fields
42
+ search_fields[assoc.table_name] = assoc.searchable_attributes
43
+ end
44
+
45
+ # Add Base Model's Search Fields
46
+ search_fields[model.table_name] = []
47
+ model.searchable_attributes.each { |a| search_fields[model.table_name] << a }
48
+
49
+ # Run through Search Fields Models
50
+ search_fields.each do |m|
51
+
52
+ # Run through Model's Fields
53
+ m.each do |f|
54
+
55
+ # Build Search Query & Arguments Array
56
+ search_q = search_q + "#{m}.#{f} LIKE ? "
57
+ search_a << "%#{search}%"
58
+ end
59
+ end
60
+
61
+ # Perform Search and eliminate doubles
62
+ search_model.where(search_q, *search_a).references(model.table_name).uniq
63
+ end
64
+
65
+ # Private
66
+ private
67
+
68
+ ##################
69
+ # Filter Methods #
70
+ ##################
71
+
72
+ # Authentication Filter
73
+ def auth_filter
74
+ redirect_to new_session_url, alert: { head: t('corn_starch.auth.access_denied'), body: t('corn_starch.auth.login_required') } unless is_authed
75
+ end
76
+
77
+ # Un-Authentication Filter
78
+ def unauth_filter
79
+ redirect_to root_url, alert: { head: t('corn_starch.auth.access_denied'), body: t('corn_starch.auth.already_logged_in') } if is_authed
80
+ end
81
+
82
+ # Self Filter
83
+ def self_filter
84
+ redirect_to root_url, alert: { head: t('corn_starch.auth.access_denied'), body: t('corn_starch.auth.invalid_user_id') } unless is_self
85
+ end
86
+
87
+ # Not Self Filter
88
+ def not_self_filter
89
+ redirect_to root_url, alert: { head: t('corn_starch.auth.access_denied'), body: t('corn_starch.auth.invalid_user_id') } if is_self
90
+ end
91
+
92
+ # Pagination Filter
93
+ def pagination_filter
94
+
95
+ # Set Records Per Page from Request Arguments
96
+ session[:pagination][:recs_per_page] = params[:records_per_page].to_i if params[:records_per_page]
97
+
98
+ # Init Pagination Vars
99
+ @pagination[:current] = 0
100
+ @pagination[:links] = []
101
+ @pagination[:pg_count] = 0
102
+
103
+ # Acquire Records per Page from Session / Configuration
104
+ @pagination[:recs_per_page] = (session[:pagination][:recs_per_page] || CORNSTARCH_CONF['pagination']['records_per_page']['default']).to_i
105
+ end
106
+
107
+ ##################
108
+ # Helper Methods #
109
+ ##################
110
+
111
+ # User Model
112
+ def user_model
113
+ Kernel.const_get CORNSTARCH_CONF['authentication']['user_model']['name']
114
+ end
115
+
116
+ # UID Field
117
+ def uid_field
118
+ CORNSTARCH_CONF['authentication']['user_model']['uid_field']
119
+ end
120
+
121
+ # Current User
122
+ def current_user
123
+ @current_user ||= user_model.find(session[:user_id]) if session[:user_id]
124
+ end
125
+
126
+ # Is Authed
127
+ def is_authed
128
+ current_user != nil
129
+ end
130
+
131
+ # Is Self
132
+ def is_self
133
+ return false unless current_user
134
+ params[:id] == current_user.id.to_s
135
+ end
136
+
137
+ # Paginate
138
+ def paginate collection, params
139
+
140
+ # Acquire Current Page
141
+ current = (params[:current_page] || 0).to_i
142
+
143
+ # Compute Page Count
144
+ total = collection.count
145
+ @pagination[:pg_count] = (total / @pagination[:recs_per_page]) + (((total % @pagination[:recs_per_page]) > 0) ? 1 : 0)
146
+
147
+ # Compute Page Range
148
+ range_start = current - (CORNSTARCH_CONF['pagination']['page_range'] / 2)
149
+ range_start = 0 if range_start < 0
150
+ range_end = current + (CORNSTARCH_CONF['pagination']['page_range'] / 2)
151
+ range_end = @pagination[:pg_count] - 1 if range_end >= @pagination[:pg_count]
152
+
153
+ # Create Page Links
154
+ @pagination[:links] = range_start .. range_end
155
+ @pagination[:current] = current
156
+
157
+ # Fetch Records
158
+ collection.limit(@pagination[:recs_per_page]).offset current * @pagination[:recs_per_page]
159
+ end
160
+
161
+ # Generic Notice Messages
162
+ def notice_msg model, entity, action, result_or_name
163
+
164
+ # Prepare Names
165
+ uname = current_user ? current_user.username : ''
166
+ ename = entity ? entity.display_name : ''
167
+
168
+ # Switch on Action
169
+ case action
170
+
171
+ # Session Control
172
+ when :login
173
+ {
174
+ pass: { head: t('corn_starch.gen_msg.login.pass.head'), body: t('corn_starch.gen_msg.login.pass.body', name: uname) },
175
+ fail: { head: t('corn_starch.gen_msg.login.fail.head'), body: t('corn_starch.gen_msg.login.fail.body') }
176
+ }[result_or_name]
177
+ when :logout
178
+ { head: t('corn_starch.gen_msg.logout.head'), body: t('corn_starch.gen_msg.logout.body') }
179
+
180
+ # Generic Resources
181
+ when :create
182
+ {
183
+ pass: { head: t('corn_starch.gen_msg.create.pass.head', model: model), body: t('corn_starch.gen_msg.create.pass.body', model: model, name: ename) },
184
+ fail: { head: t('corn_starch.gen_msg.create.fail.head', model: model), body: t('corn_starch.gen_msg.create.fail.body') }
185
+ }[result_or_name]
186
+ when :update
187
+ {
188
+ pass: { head: t('corn_starch.gen_msg.update.pass.head', model: model), body: t('corn_starch.gen_msg.update.pass.body', model: model, name: ename) },
189
+ fail: { head: t('corn_starch.gen_msg.update.fail.head', model: model), body: t('corn_starch.gen_msg.update.fail.body') }
190
+ }[result_or_name]
191
+ when :destroy
192
+ { head: t('corn_starch.gen_msg.destroy.head', model: model), body: t('corn_starch.gen_msg.destroy.body', model: model, name: result_or_name) }
193
+
194
+ # Error
195
+ else
196
+ t('corn_starch.gen_msg.unknown_action', action: action)
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,36 @@
1
+ # CornStarch
2
+ # by Eresse <eresse@eresse.net>
3
+
4
+ # CornStarch Module
5
+ module CornStarch
6
+
7
+ # Sessions Controller
8
+ class SessionsController < ApplicationController
9
+
10
+ # Filters
11
+ skip_before_filter :auth_filter, only: [:new, :create]
12
+ before_filter :unauth_filter, only: [:new, :create]
13
+
14
+ # New (Login)
15
+ def new
16
+ end
17
+
18
+ # Create (Login)
19
+ def create
20
+ @user = user_model.find_by uid_field => params[uid_field]
21
+ if @user && @user.authenticate(params[:password])
22
+ session[:user_id] = @user.id
23
+ redirect_to root_url, notice: notice_msg(nil, nil, :login, :pass)
24
+ else
25
+ flash.now[:alert] = notice_msg(nil, nil, :login, :fail)
26
+ render action: :new
27
+ end
28
+ end
29
+
30
+ # Drop (Logout)
31
+ def drop
32
+ session[:user_id] = nil
33
+ redirect_to root_url, notice: notice_msg(nil, nil, :logout, nil)
34
+ end
35
+ end
36
+ end
@@ -11,9 +11,49 @@ module CornStarch
11
11
  extend ActionView::Helpers
12
12
  extend ActionView::Context
13
13
 
14
- # Include Stylesheets
15
- def self.corn_starch_resource_tags
16
- stylesheet_link_tag('bootstrap.min') + javascript_include_tag('bootstrap.min')
14
+ # Class Masher
15
+ def class_mash base, classes
16
+ class_s = base
17
+ if classes.respond_to? :each
18
+ classes.each { |c| class_s += " #{base}-#{c}" }
19
+ else
20
+ class_s += " #{base}-#{classes}"
21
+ end
22
+ class_s
23
+ end
24
+
25
+ # Glyph Icon
26
+ def glyphicon_tag icon_class
27
+ span_tag nil, class: class_mash(:glyphicon, icon_class), 'arya-hidden': true
28
+ end
29
+
30
+ # Label
31
+ def lbl_tag label_class, content, icon = nil
32
+ span_tag nil, class: class_mash(:label, label_class) do
33
+ icon ? glyphicon_tag(icon) + ' ' + content : content
34
+ end
35
+ end
36
+
37
+ # Button Link
38
+ def btn_link_to btn_class, target, name = nil, icon = nil, options = {}
39
+ link_to target, options do
40
+ button_tag class: class_mash(:btn, btn_class) do
41
+ block_given? ? yield : (icon ? glyphicon(icon) + ' ' + name : name)
42
+ end
43
+ end
44
+ end
45
+
46
+ # Missing Method Handler
47
+ def method_missing name, *args, &block
48
+
49
+ # Handle Generic Tag
50
+ if name.to_s =~ /(.*)_tag/
51
+ content_tag $1.to_sym, *args, &block
52
+
53
+ # Default
54
+ else
55
+ super
56
+ end
17
57
  end
18
58
  end
19
- end
59
+ end
@@ -0,0 +1,7 @@
1
+ <!-- Display Flash Notifications -->
2
+ <% flash.each do |name, msg| %>
3
+ <%= content_tag :div, class: "alert alert-#{{ notice: 'success', alert: 'danger' }[name.to_sym]}", role: :alert do %>
4
+ <strong><%= msg['head'] %></strong>
5
+ <p><%= msg['body'] %></p>
6
+ <% end %>
7
+ <% end %>
@@ -0,0 +1,21 @@
1
+ <!-- Page Links -->
2
+ <nav>
3
+ <%= ul_tag class: :corn_starch_pagination do %>
4
+ <%= li_tag link_to('<<', current_page: 0) %>
5
+ <% @pagination[:links].each do |p| %>
6
+ <%= li_tag link_to(p, (p == @pagination[:current] ? '#' : {current_page: p})), class: (p == @pagination[:current] ? :active : '') %>
7
+ <% end %>
8
+ <%= li_tag link_to('>>', current_page: @pagination[:pg_count] - 1) %>
9
+ <% end %>
10
+ </nav>
11
+
12
+ <!-- Records per Page Selector -->
13
+ <form method='get' action='' class='form-inline'>
14
+ <div class='form-group'>
15
+ <%= label_tag :records_per_page, 'Records per page', class: 'control-label' %>
16
+ <div class='input-group'>
17
+ <%= select_tag :records_per_page, options_for_select(CORNSTARCH_CONF['pagination']['records_per_page']['opts'], @pagination[:recs_per_page]), class: 'form-control' %>
18
+ </div>
19
+ </div>
20
+ <%= submit_tag 'Go', name: nil, class: 'btn btn-primary' %>
21
+ </form>
@@ -0,0 +1,33 @@
1
+ # CornStarch
2
+ # by Eresse <eresse@eresse.net>
3
+
4
+ # CornStarch Configuration Initializer
5
+
6
+ # Includes
7
+ require 'rubygems'
8
+ require 'yaml'
9
+ require 'json'
10
+
11
+ # Load up Configuration
12
+ CORNSTARCH_CONF = {}
13
+ CORNSTARCH_CONF_FILE = "#{Rails.root}/config/corn_starch.yml"
14
+ CORNSTARCH_CONF = YAML::load_file CORNSTARCH_CONF_FILE if File.exist? CORNSTARCH_CONF_FILE
15
+
16
+ ######################
17
+ # Configure Defaults #
18
+ ######################
19
+
20
+ # Base Style
21
+ CORNSTARCH_CONF['base_style'] ||= 'bootstrap'
22
+
23
+ # Pagination
24
+ CORNSTARCH_CONF['pagination'] ||= {}
25
+ CORNSTARCH_CONF['pagination']['page_range'] ||= 6
26
+ CORNSTARCH_CONF['pagination']['records_per_page'] ||= {}
27
+ CORNSTARCH_CONF['pagination']['records_per_page']['default'] ||= 10
28
+ CORNSTARCH_CONF['pagination']['records_per_page']['opts'] ||= [ 10, 25, 50, 100 ]
29
+
30
+ # Authentication
31
+ CORNSTARCH_CONF['authentication'] ||= {}
32
+ CORNSTARCH_CONF['authentication']['user_model']['name'] = 'User'
33
+ CORNSTARCH_CONF['authentication']['user_model']['uid_field'] = 'username'
@@ -0,0 +1,58 @@
1
+ # CornStarch
2
+ # by Eresse <eresse@eresse.net>
3
+
4
+ # English Translations
5
+ en:
6
+
7
+ # CornStarch Strings
8
+ corn_starch:
9
+
10
+ # Auth
11
+ auth:
12
+ access_denied: "Access denied"
13
+ login_required: "Login required"
14
+ already_logged_in: "Already logged in"
15
+ invalid_user_id: "Invalid user ID"
16
+
17
+ # Generic Notice Messages
18
+ gen_msg:
19
+
20
+ # Login
21
+ login:
22
+ pass:
23
+ head: "Logged in"
24
+ body: "You have successfully logged in as %{name}"
25
+ fail:
26
+ head: "Login failed"
27
+ body: "Invalid username / password"
28
+
29
+ # Logout
30
+ logout:
31
+ head: "Logged out"
32
+ body: "You have successfully logged out"
33
+
34
+ # Create
35
+ create:
36
+ pass:
37
+ head: "%{model} created"
38
+ body: "%{model} [%{name}] was successfully created"
39
+ fail:
40
+ head: "%{model} creation failed"
41
+ body: "Invalid information"
42
+
43
+ # Update
44
+ update:
45
+ pass:
46
+ head: "%{model} updated"
47
+ body: "%{model} [%{name}] was successfully updated"
48
+ fail:
49
+ head: "%{model} update failed"
50
+ body: "Invalid information"
51
+
52
+ # Destroy
53
+ destroy:
54
+ head: "%{model} destroyed"
55
+ body: "%{model} [%{name}] was successfully destroyed"
56
+
57
+ # Unknown Action
58
+ unknown_action: "Unknown action [%{action}]"
@@ -0,0 +1,51 @@
1
+ # CornStarch
2
+ # by Eresse <eresse@eresse.net>
3
+
4
+ # French Translations
5
+ fr:
6
+
7
+ # CornStarch Strings
8
+ corn_starch:
9
+
10
+ # Generic Notice Messages
11
+ gen_msg:
12
+
13
+ # Login
14
+ login:
15
+ pass:
16
+ head: ""
17
+ body: ""
18
+ fail:
19
+ head: ""
20
+ body: ""
21
+
22
+ # Logout
23
+ logout:
24
+ head: ""
25
+ body: ""
26
+
27
+ # Create
28
+ create:
29
+ pass:
30
+ head: ""
31
+ body: ""
32
+ fail:
33
+ head: ""
34
+ body: ""
35
+
36
+ # Update
37
+ update:
38
+ pass:
39
+ head: ""
40
+ body: ""
41
+ fail:
42
+ head: ""
43
+ body: ""
44
+
45
+ # Destroy
46
+ destroy:
47
+ head: ""
48
+ body: ""
49
+
50
+ # Unknown Action
51
+ unknown_action: ""