bizarroids_settings 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.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +77 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/bizarroids/application.js +13 -0
  6. data/app/assets/stylesheets/bizarroids/application.css +15 -0
  7. data/app/controllers/bizarroids/settings/application_controller.rb +4 -0
  8. data/app/controllers/bizarroids/settings/options_controller.rb +33 -0
  9. data/app/helpers/bizarroids/settings/options_helper.rb +15 -0
  10. data/app/helpers/bizarroids/settings/settings_helper.rb +27 -0
  11. data/app/models/bizarroids/settings/option.rb +65 -0
  12. data/app/views/bizarroids/settings/options/edit.html.erb +21 -0
  13. data/app/views/bizarroids/settings/options/index.html.erb +42 -0
  14. data/app/views/layouts/bizarroids/settings/application.html.erb +14 -0
  15. data/config/routes.rb +7 -0
  16. data/db/schema.rb +38 -0
  17. data/lib/bizarroids/settings/bizarroids_settings_option_uploader.rb +29 -0
  18. data/lib/bizarroids/settings/config_error.rb +4 -0
  19. data/lib/bizarroids/settings/engine.rb +7 -0
  20. data/lib/bizarroids/settings/helpers/settings_helper.rb +27 -0
  21. data/lib/bizarroids/settings/version.rb +5 -0
  22. data/lib/bizarroids/settings.rb +123 -0
  23. data/lib/bizarroids.rb +4 -0
  24. data/lib/bizarroids_settings.rb +1 -0
  25. data/lib/generators/bizarroids/settings/install/USAGE +14 -0
  26. data/lib/generators/bizarroids/settings/install/install_generator.rb +23 -0
  27. data/lib/generators/bizarroids/settings/install/templates/bizarroids_settings.en.yml +30 -0
  28. data/lib/generators/bizarroids/settings/install/templates/bizarroids_settings.ru.yml +31 -0
  29. data/lib/generators/bizarroids/settings/install/templates/initializer.rb +34 -0
  30. data/lib/generators/bizarroids/settings/install/templates/options_migration.rb +29 -0
  31. data/lib/tasks/bizarroids_tasks.rake +4 -0
  32. data/test/bizarroids_test.rb +7 -0
  33. data/test/controllers/bizarroids/settings/options_controller_test.rb +9 -0
  34. data/test/dummy/README.rdoc +28 -0
  35. data/test/dummy/Rakefile +6 -0
  36. data/test/dummy/app/assets/javascripts/application.js +13 -0
  37. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  38. data/test/dummy/app/controllers/application_controller.rb +11 -0
  39. data/test/dummy/app/controllers/home_controller.rb +4 -0
  40. data/test/dummy/app/helpers/application_helper.rb +2 -0
  41. data/test/dummy/app/models/ability.rb +9 -0
  42. data/test/dummy/app/views/home/index.html.erb +1 -0
  43. data/test/dummy/app/views/layouts/application.html.erb +21 -0
  44. data/test/dummy/bin/bundle +3 -0
  45. data/test/dummy/bin/rails +4 -0
  46. data/test/dummy/bin/rake +4 -0
  47. data/test/dummy/config/application.rb +23 -0
  48. data/test/dummy/config/boot.rb +5 -0
  49. data/test/dummy/config/database.yml +25 -0
  50. data/test/dummy/config/environment.rb +5 -0
  51. data/test/dummy/config/environments/development.rb +37 -0
  52. data/test/dummy/config/environments/production.rb +82 -0
  53. data/test/dummy/config/environments/test.rb +39 -0
  54. data/test/dummy/config/initializers/assets.rb +8 -0
  55. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  56. data/test/dummy/config/initializers/bizarroids_settings.rb +39 -0
  57. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  58. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  59. data/test/dummy/config/initializers/inflections.rb +16 -0
  60. data/test/dummy/config/initializers/mime_types.rb +4 -0
  61. data/test/dummy/config/initializers/session_store.rb +3 -0
  62. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  63. data/test/dummy/config/locales/bizarroids_settings.en.yml +30 -0
  64. data/test/dummy/config/locales/bizarroids_settings.ru.yml +31 -0
  65. data/test/dummy/config/locales/en.yml +23 -0
  66. data/test/dummy/config/routes.rb +7 -0
  67. data/test/dummy/config/secrets.yml +22 -0
  68. data/test/dummy/config.ru +4 -0
  69. data/test/dummy/db/development.sqlite3 +0 -0
  70. data/test/dummy/db/migrate/20140731121008_create_bizarroids_settings_options.rb +29 -0
  71. data/test/dummy/db/schema.rb +39 -0
  72. data/test/dummy/db/test.sqlite3 +0 -0
  73. data/test/dummy/log/development.log +51360 -0
  74. data/test/dummy/log/test.log +94685 -0
  75. data/test/dummy/public/404.html +67 -0
  76. data/test/dummy/public/422.html +67 -0
  77. data/test/dummy/public/500.html +66 -0
  78. data/test/dummy/public/favicon.ico +0 -0
  79. data/test/dummy/public/system/settings/file_option/0_832a7_af94124e_-1-L.jpg +0 -0
  80. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  81. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  82. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  83. data/test/dummy/tmp/cache/assets/development/sprockets/48917139f539fdb146b469bb51d3d8a9 +0 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/51ba48ea70bfd46c0656e1d96ef04eab +0 -0
  85. data/test/dummy/tmp/cache/assets/development/sprockets/9ce90cea0febc5c40bd72ea48e76bff7 +0 -0
  86. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  88. data/test/dummy/tmp/cache/assets/development/sprockets/dd426b5348faf19a98e0326dc07bec37 +0 -0
  89. data/test/dummy/tmp/cache/assets/development/sprockets/e7a8c6fc20a213fe7e6dbe204e74166b +0 -0
  90. data/test/dummy/tmp/cache/assets/development/sprockets/edabf3e4354e52066357ed9706cc6b9c +0 -0
  91. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  92. data/test/dummy/tmp/generators/config/initializers/bizarroids_settings.rb +34 -0
  93. data/test/dummy/tmp/generators/config/locales/bizarroids_settings.en.yml +30 -0
  94. data/test/dummy/tmp/generators/config/locales/bizarroids_settings.ru.yml +31 -0
  95. data/test/dummy/tmp/generators/config/routes.rb +7 -0
  96. data/test/dummy/tmp/generators/db/migrate/20140802143121_create_bizarroids_settings_options.rb +29 -0
  97. data/test/fixtures/bizarroids/settings/options.yml +5 -0
  98. data/test/helpers/bizarroids/settings/options_helper_test.rb +17 -0
  99. data/test/integration/navigation_test.rb +10 -0
  100. data/test/lib/bizarroids/settings_test.rb +151 -0
  101. data/test/lib/generators/bizarroids/bizarroids/settings/install_generator_test.rb +35 -0
  102. data/test/models/bizarroids/settings/option_test.rb +48 -0
  103. data/test/test_helper.rb +47 -0
  104. metadata +316 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 59811ae2e4943af1f4bf06dd4dd10bbda512d03b
4
+ data.tar.gz: ded645fc5f175f9bd39aeac14e46ccce006e8404
5
+ SHA512:
6
+ metadata.gz: bf46de55f1db8143b409d49af36218308457460062735ea4e340e922d9341f84ae642fc9812848537ebb43fe14aa19d93851a2ac31c14743ed223200edbf0e43
7
+ data.tar.gz: 922087c719a1306aefb58e7ff372394fb0073fbb9e9dd529bba1686ad539a2d3152fcdf399bd21042d781b1fc0cbfb794f77cc353e1302967e7cc1b77e42af37
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Corlinus
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,77 @@
1
+ = Bizarroids Settings
2
+
3
+ Flexible settings for Rails application. This is a part of The Bizarroids.
4
+
5
+ == Installation
6
+
7
+ Add it to your Gemfile:
8
+
9
+ gem 'bizarroids_settings'
10
+
11
+ Run the following command to install it:
12
+
13
+ bundle install
14
+
15
+ Run the generator:
16
+
17
+ rails generate bizarroids:settings:install
18
+
19
+ == Configuraton
20
+
21
+ Edit +config/initialisers/bizarroids_settings.rb+. You can create options using folowing syntax:
22
+
23
+ config.option :key, :string, value: 'MyString', other: :options
24
+
25
+ Avaliable types:
26
+
27
+ - string
28
+ - integer
29
+ - float
30
+ - boolean
31
+ - text
32
+ - date
33
+ - datetime
34
+ - file
35
+
36
+ Avaliable options:
37
+
38
+ * name - gives user readable name to the option. It would be show in admin panel.
39
+ * description - description to show in admin panel.
40
+ * require - if set true, prevents user to set empty value.
41
+ * hidden - if set true, hides option from user in admin panel.
42
+ * collection - (+collection: [1, 2, 3]+) forses user to select given values instead of enter in input field.
43
+
44
+ Some additional configuration can be found +config/initialisers/bizarroids_settings.rb+
45
+
46
+ == Usage
47
+
48
+ Link for admin panel
49
+
50
+ bizarroids_settings_menu_link
51
+
52
+ If you use cancancan place in your ability.rb somthing like
53
+
54
+ can :manage, Bizarroids::Settings::Option
55
+
56
+ if don't, place in config/initialisers/bizarroids_settings.rb
57
+
58
+ config.use_cancancan = false
59
+
60
+ To get value for :key
61
+
62
+ Get:
63
+
64
+ Bizarroids::Settings.get(:key)
65
+ Bizarroids::Settings[:key]
66
+
67
+ Set:
68
+
69
+ Bizarroids::Settings.get(:key) = 'my value'
70
+ Bizarroids::Settings[:key] = 'my value'
71
+
72
+ == TODO
73
+
74
+ * image styles for file field
75
+ * Tests
76
+
77
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'BizarroidsSettings'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module Bizarroids::Settings
2
+ class ApplicationController < parent_controller.constantize
3
+ end
4
+ end
@@ -0,0 +1,33 @@
1
+ require_dependency "bizarroids/settings/application_controller"
2
+
3
+ module Bizarroids::Settings
4
+ class OptionsController < parent_controller.constantize
5
+ helper 'bizarroids/settings/options'
6
+
7
+ respond_to :html
8
+ inherit_resources
9
+ actions :index, :edit, :update
10
+
11
+ if Bizarroids::Settings.use_cancancan
12
+ authorize_resource class: "Bizarroids::Settings::Option", param_method: :permitted_params
13
+ end
14
+
15
+ def update
16
+ update!(notice: t('bizarroids.settings.option_updated')) { bizarroids_settings_engine.options_url }
17
+ end
18
+
19
+ protected
20
+
21
+ def resource
22
+ @option ||= end_of_association_chain.user_editable.find_by(key: params[:id])
23
+ end
24
+
25
+ def collection
26
+ @options ||= end_of_association_chain.user_editable
27
+ end
28
+
29
+ def permitted_params
30
+ params.permit(bizarroids_settings_option: Bizarroids::Settings::VALUE_TYPES.map {|x| :"#{x}_value"} + [:remove_file_value])
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ module Bizarroids::Settings::OptionsHelper
2
+ def bizarroids_settings_input_options custom_options={}
3
+ key = @option.sym_key
4
+ {
5
+ label: @option.human_name,
6
+ required: @option.required,
7
+ input_html: { class: Bizarroids::Settings.options[key][:input_class] },
8
+ collection: @option.collection? ? bizarroids_settings_select_collection(key) : nil
9
+ }.merge custom_options
10
+ end
11
+
12
+ def bizarroids_settings_select_collection key
13
+ Bizarroids::Settings.options[key][:collection]
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ module Bizarroids::Settings
2
+ module SettingsHelper
3
+ def bizarroids_edit_button path
4
+ link_to path, class: 'btn btn-link btn-xs text-primary' do
5
+ content_tag :span, '', class: 'glyphicon glyphicon-pencil'
6
+ end
7
+ end
8
+
9
+ def bizarroids_t key
10
+ t key, scope: :bizarroids
11
+ end
12
+
13
+ def bizarroids_settings_menu_link
14
+ link_to bizarroids_t('settings.name'),
15
+ bizarroids_settings_engine.options_path,
16
+ class: [bizarroids_settings_menu_link_class, 'list-group-item']
17
+ end
18
+
19
+ def bizarroids_settings_menu_link_class
20
+ bizarroids_settings_menu_link_active? ? :active : nil
21
+ end
22
+
23
+ def bizarroids_settings_menu_link_active?
24
+ controller.kind_of?(Bizarroids::Settings::OptionsController)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,65 @@
1
+ module Bizarroids::Settings
2
+ class Option < ActiveRecord::Base
3
+ validates :key, presence: true, uniqueness: true
4
+ validates :value_type, presence: true, inclusion: { in: Bizarroids::Settings::VALUE_TYPES }
5
+
6
+ mount_uploader :file_value, ::Bizarroids::Settings::OptionUploader
7
+
8
+ def self.active_attr attr_name
9
+ :"#{attr_name}_value"
10
+ end
11
+
12
+ Bizarroids::Settings::VALUE_TYPES.each do |type|
13
+ # validates :string_value, presence: true, if: "value_type == 'string' && required?"
14
+ validates self.active_attr(type), presence: true, if: "value_type == '#{type}' && required?"
15
+ end
16
+
17
+ scope :visible, -> { where hidden: false }
18
+ scope :actual, -> { where key: Bizarroids::Settings.keys }
19
+ scope :user_editable, -> { actual.visible }
20
+ scope :default_order, -> { order :position }
21
+ default_scope -> { default_order }
22
+
23
+ def to_param
24
+ key
25
+ end
26
+
27
+ def sym_key
28
+ key.to_sym
29
+ end
30
+
31
+ def active_attr
32
+ self.class.active_attr value_type
33
+ end
34
+
35
+ def get_value
36
+ send active_attr
37
+ end
38
+
39
+ def set_value val
40
+ update_attributes value: val
41
+ end
42
+
43
+ def avaliable_values
44
+ Bizarroids::Settings.options[sym_key][:collection]
45
+ end
46
+
47
+ def human_name
48
+ name || key.humanize
49
+ end
50
+
51
+ def human_type
52
+ I18n.t value_type, scope: 'bizarroids.settings.types'
53
+ end
54
+
55
+ attr_accessor :value
56
+
57
+ before_validation do
58
+ self.value_type = self.value_type.to_s
59
+
60
+ if value.present?
61
+ send :"#{active_attr}=", self.value
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,21 @@
1
+ <%= content_for :page_header do %>
2
+ <h1>
3
+ <%= bizarroids_t 'settings.name' %>
4
+ <small><%= bizarroids_t :edit %> &mdash; <%= @option.human_name %></small>
5
+ </h1>
6
+ <% end %>
7
+
8
+ <div class='well'>
9
+ <%= simple_form_for @option, url: bizarroids_settings_engine.option_path(@option), html: { class: 'form-horizontal' } do |f| %>
10
+ <%= f.input @option.active_attr, bizarroids_settings_input_options %>
11
+ <% if @option.value_type == 'file' %>
12
+ <%= f.input :remove_file_value, as: :boolean %>
13
+ <% end %>
14
+ <div class='form-group'>
15
+ <div class='col-lg-offset-2 col-lg-10'>
16
+ <%= f.submit bizarroids_t(:save), class: 'btn btn-primary' %>
17
+ <%= link_to bizarroids_t(:cancel), bizarroids_settings_engine.options_path, class: 'btn btn-link' %>
18
+ </div>
19
+ </div>
20
+ <% end %>
21
+ </div>
@@ -0,0 +1,42 @@
1
+ <%= content_for :page_header do %>
2
+ <h1><%= bizarroids_t 'settings.name' %></h1>
3
+ <% end %>
4
+
5
+ <table class='table table-bordered table-striped'>
6
+ <thead>
7
+ <tr>
8
+ <% %i(name value_type value).each do |attr_name| %>
9
+ <th><%= resource_class.human_attribute_name(attr_name) %></th>
10
+ <% end %>
11
+ <th>&nbsp;</th>
12
+ </tr>
13
+ </thead>
14
+ <tbody>
15
+ <% @options.each do |option| %>
16
+ <tr>
17
+ <td>
18
+ <%= link_to option.human_name, bizarroids_settings_engine.edit_option_path(option) %>
19
+ <% if option.description.present? %>
20
+ <br/><em><%= option.description %></em>
21
+ <% end %>
22
+ </td>
23
+ <td><%= option.human_type %></td>
24
+ <td>
25
+ <% if option.get_value.present? %>
26
+ <% case option.value_type %>
27
+ <% when 'date', 'datetime' %>
28
+ <%= l option.get_value, format: :long %>
29
+ <% when 'file' %>
30
+ <%= link_to h(truncate(File.basename(option.get_value.path), length: 30)), option.get_value.url, target: :_blank %>
31
+ <% else %>
32
+ <%= option.get_value %>
33
+ <% end %>
34
+ <% else %>
35
+ <em><%= bizarroids_t :not_set %></em>
36
+ <% end %>
37
+ </td>
38
+ <td class='text-right'><%= bizarroids_edit_button bizarroids_settings_engine.edit_option_path(option) %></td>
39
+ </tr>
40
+ <% end %>
41
+ </tbody>
42
+ </table>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Bizarroids</title>
5
+ <%= stylesheet_link_tag "bizarroids/application", media: "all" %>
6
+ <%= javascript_include_tag "bizarroids/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ Bizarroids::Settings::Engine.routes.draw do
2
+ scope module: :bizarroids do
3
+ scope module: :settings do
4
+ resources :options, only: %i(index edit update)
5
+ end
6
+ end
7
+ end
data/db/schema.rb ADDED
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20140728085200) do
15
+
16
+ create_table "bizarroids_settings_options", force: true do |t|
17
+ t.string "key"
18
+ t.string "value_type", limit: 10
19
+ t.string "name"
20
+ t.string "description"
21
+ t.boolean "required", default: false
22
+ t.boolean "hidden", default: false
23
+ t.boolean "collection", default: false
24
+ t.integer "position"
25
+ t.string "string_value"
26
+ t.integer "integer_value"
27
+ t.float "float_value"
28
+ t.boolean "boolean_value", default: false
29
+ t.text "text_value"
30
+ t.date "date_value"
31
+ t.datetime "datetime_value"
32
+ t.datetime "created_at"
33
+ t.datetime "updated_at"
34
+ end
35
+
36
+ add_index "bizarroids_settings_options", ["key"], name: "index_bizarroids_settings_options_on_key"
37
+
38
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ module Bizarroids::Settings
4
+ class OptionUploader < CarrierWave::Uploader::Base
5
+ include CarrierWave::MiniMagick
6
+
7
+ storage :file
8
+
9
+ def store_dir
10
+ File.join Bizarroids::Settings.files_storage_dir, model.key
11
+ end
12
+
13
+ # Process files as they are uploaded:
14
+ # process :scale => [200, 300]
15
+ #
16
+ # def scale(width, height)
17
+ # # do something
18
+ # end
19
+
20
+ # Create different versions of your uploaded files:
21
+ # version :thumb do
22
+ # process :resize_to_fit => [50, 50]
23
+ # end
24
+
25
+ def extension_white_list
26
+ Bizarroids::Settings.options[model.sym_key][:extension_white_list]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module Bizarroids::Settings
2
+ class ConfigError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ require 'bizarroids/settings/helpers/settings_helper'
2
+ module Bizarroids
3
+ module Settings
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module Bizarroids::Settings
2
+ module SettingsHelper
3
+ def bizarroids_edit_button path
4
+ link_to path, class: 'btn btn-link btn-xs text-primary' do
5
+ content_tag :span, '', class: 'glyphicon glyphicon-pencil'
6
+ end
7
+ end
8
+
9
+ def bizarroids_t key
10
+ t key, scope: :bizarroids
11
+ end
12
+
13
+ def bizarroids_settings_menu_link
14
+ link_to Bizarroids::Settings::Option.model_name.human(count: 10),
15
+ bizarroids_settings_engine.options_path,
16
+ class: bizarroids_settings_menu_link_class
17
+ end
18
+
19
+ def bizarroids_settings_menu_link_class
20
+ bizarroids_settings_menu_link_active? ? :active : nil
21
+ end
22
+
23
+ def bizarroids_settings_menu_link_active?
24
+ controller.kind_of?(Bizarroids::Settings::OptionsController)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Bizarroids
2
+ module Settings
3
+ VERSION = "0.1.2".freeze
4
+ end
5
+ end
@@ -0,0 +1,123 @@
1
+ require 'cancancan'
2
+ require 'inherited_resources'
3
+ require 'simple_form'
4
+ require 'mini_magick'
5
+ require 'carrierwave'
6
+
7
+ require 'bizarroids/settings/helpers/settings_helper'
8
+ require 'bizarroids/settings/engine'
9
+ require 'bizarroids/settings/config_error'
10
+ require 'bizarroids/settings/bizarroids_settings_option_uploader'
11
+
12
+ module Bizarroids
13
+ module Settings
14
+ # name prefix for ActiveRecord
15
+ def self.table_name_prefix
16
+ 'bizarroids_settings_'
17
+ end
18
+
19
+ VALUE_TYPES = %w(string integer float boolean text date datetime file).freeze
20
+
21
+ def self.get key
22
+ raise ActiveRecord::RecordNotFound.new("Option ':#{key}' not defined") unless key.in? keys
23
+ Option.find_by!(key: key).try :get_value
24
+ end
25
+
26
+ def self.set key, value
27
+ raise ActiveRecord::RecordNotFound.new("Option ':#{key}' not defined") unless key.in? keys
28
+ Option.find_by!(key: key).set_value value
29
+ end
30
+
31
+ class << self
32
+ alias :[] :get
33
+ alias :[]= :set
34
+ end
35
+
36
+ mattr_accessor :options
37
+ @@options = {}
38
+
39
+ mattr_accessor :keys
40
+ @@keys = []
41
+
42
+ mattr_accessor :parent_controller
43
+ @@parent_controller = '::ApplicationController'
44
+
45
+ mattr_accessor :use_cancancan
46
+ @@use_cancancan = true
47
+
48
+ mattr_accessor :files_storage_dir
49
+ @@files_storage_dir = 'system/settings'
50
+
51
+ def self.setup
52
+ yield self
53
+ end
54
+
55
+ private
56
+
57
+ def self.option key, type, attrs={}
58
+ key = key.to_sym
59
+ type = type.to_sym
60
+ attrs = attrs.dup
61
+
62
+ if options.has_key? key
63
+ raise ConfigError.new "Option ':#{key}' already exists"
64
+ end
65
+
66
+ if attrs[:required] && attrs[:value].blank?
67
+ raise ConfigError.new "You must specify :value with required: true"
68
+ end
69
+
70
+ if attrs[:hidden] && attrs[:value].blank?
71
+ raise ConfigError.new "You must specify :value with hidden: true"
72
+ end
73
+
74
+ if attrs[:collection].present? && !attrs[:collection].kind_of?(Array)
75
+ raise ConfigError.new ":collection option must be an array"
76
+ end
77
+
78
+ options[key] = attrs.merge type: type
79
+ keys << key
80
+
81
+ # skip creating if table is not exists
82
+ return unless ActiveRecord::Base.connection.table_exists? Option.table_name
83
+
84
+ attrs[:collection] = !!attrs[:collection]
85
+ attrs[:position] = keys.index(key)
86
+
87
+ option = Option.find_by key: key
88
+
89
+ if option.present?
90
+ if option.value_type.to_sym != type
91
+ # replace option if type changed
92
+ option.destroy
93
+ option = nil
94
+ else
95
+ option.update_attributes attrs_for_update(attrs)
96
+ return option
97
+ end
98
+ end
99
+
100
+ if option.blank?
101
+ option = Option.create! attrs_for_create(key, type, attrs)
102
+ end
103
+
104
+ option
105
+ end
106
+
107
+ def self.attrs_for_create key, type, attrs
108
+ attrs.merge(key: key, value_type: type).slice *valid_attr_names
109
+ end
110
+
111
+ def self.attrs_for_update attrs
112
+ attrs.slice *auto_update_attr_names
113
+ end
114
+
115
+ def self.auto_update_attr_names
116
+ %i(name description required hidden collection position)
117
+ end
118
+
119
+ def self.valid_attr_names
120
+ auto_update_attr_names + %i(key value_type value)
121
+ end
122
+ end
123
+ end
data/lib/bizarroids.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "bizarroids/settings"
2
+
3
+ module Bizarroids
4
+ end
@@ -0,0 +1 @@
1
+ require 'bizarroids'
@@ -0,0 +1,14 @@
1
+ Description:
2
+ It will create a migration file
3
+
4
+ Example:
5
+ rails generate bizarroids:settings:install
6
+
7
+ This will create:
8
+ db/migrate/create_bizarroids_settings_options_table.rb
9
+ config/initialiser/bizarroids_settings.rb
10
+ config/locales/bizarroids_settings.en.yml
11
+ config/locales/bizarroids_settings.ru.yml
12
+
13
+ This will add:
14
+ routes to config/routes.rb