configurable_engine 0.3.3 → 0.4.4

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: 990bdf66a3b906927e53869aa049a82638b13791
4
- data.tar.gz: 1107460d5c031cc5b143b950966cb8548b5a5529
3
+ metadata.gz: 578bcc2a45822abd17223d78f7c7fde6a0dfe3ab
4
+ data.tar.gz: bc4d2f6a418728cbb09b0753127f7e97469a6c4e
5
5
  SHA512:
6
- metadata.gz: 204972e4fc0157f654f0900e3156dba911c11d5fd45878d2c3ec3d6cc115c25ac4b8e4d131d10b8d0a43cd000d0aeec431cb29dc9f0faf90199be5fbba50b6d4
7
- data.tar.gz: 9422ce1965bd055dc95fc0597f7945433011548172b73b144fdd39ab38aa90ff2d7f66766725f107134d6ecc20038059ad3210af49b257dd0391e4ea9c0c5153
6
+ metadata.gz: f0a4fb4d57e46d36329fc96650bf8f3be1b970978fc4594acf3bba0b1faf1a4a8dfb70ffa3332a6771db6e54d50e6e2b5e5be5e5b0ad97a4bb804785b6414c1f
7
+ data.tar.gz: bea51a20af9715fa5feea6a294fdd0008bc222abc4a8c6bf37e86eeb52ab9441996da5d573b7713591c6b4a0753cce5c2e36d194b8829d4829429404a512614b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,29 @@
1
+ ### v0.4.4 - May 21, 2014
2
+ **bug fixes**
3
+ list types can *actually* deserialize comma delimeted nested and non-nested lists
4
+ tested end-to-end but need better specs. sorry about the mess.
5
+
6
+
7
+ ### v0.4.3 - May 21, 2014
8
+ **bug fixes**
9
+ list types can deserialize comma delimeted nested and non-nested lists
10
+
11
+ **in other news**
12
+ ... time to learn how to release beta versions. If I have to release another fix today, I'll do that.
13
+
14
+ ### v0.4.2 - May 21, 2014
15
+ **bug fixes**
16
+ list types can serialize comma delimeted nested and non-nested lists
17
+
18
+ ### v0.4.1 - May 21, 2014
19
+ **bug fixes**
20
+ if cacheing was enabled, configurable engine would never return default for unset values.
21
+
22
+ ### v0.4.0 - May 21, 2014
23
+ **features**
24
+ Configurable.[]= will serialize arrays set to list-type params.
25
+ BREAKING CHANGE: Configurable#value and Configurable.[] should return similarly cast values (previously #value was always the string value)
26
+
1
27
  ### v0.3.3 - May 21, 2014
2
28
  **bug fixes**
3
29
  No longer uses mass assignment internally.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Configurable [![Build Status](https://travis-ci.org/paulca/configurable_engine.png?branch=master)](https://travis-ci.org/paulca/configurable_engine)
2
2
 
3
- A Rails 3 configuration engine. An update to [Behavior](http://github.com/paulca/behavior) for Rails 3.
3
+ A Rails 3/4 configuration engine. An update to [Behavior](http://github.com/paulca/behavior) for Rails 3 and 4.
4
4
 
5
5
  ## How it works ##
6
6
 
@@ -40,7 +40,7 @@ Since Configurable is an ActiveRecord model, if you want to update the config, c
40
40
 
41
41
  ```ruby
42
42
  Configurable.create!(:name => 'site_title', :value => 'My New Site')
43
- ```
43
+ ```
44
44
  You can set the `type` attribute to `boolean`, `decimal`,`integer`, or `list` and it will treat those fields as those types. Lists are comma and/or newline delimeted arrays of strings.
45
45
 
46
46
  ## Web Interface ##
@@ -59,13 +59,20 @@ and include `ConfigurableEngine::ConfigurablesController`, eg.
59
59
  class Admin::ConfigurablesController < ApplicationController
60
60
  # include the engine controller actions
61
61
  include ConfigurableEngine::ConfigurablesController
62
-
62
+
63
63
  # add your own filter(s) / layout
64
64
  before_filter :protect_my_code
65
65
  layout 'admin'
66
66
  end
67
67
  ```
68
68
 
69
+ To ensure text areas are rendered correctly, ensure that your layout preserves whitespace. In haml, use the `~` operator
70
+
71
+ ```haml
72
+ %container
73
+ ~ yield
74
+ ```
75
+
69
76
  If you want to control how the fields in the admin interface appear, you can add additional params in your configurable.yml file:
70
77
 
71
78
  ```yaml
@@ -100,7 +107,7 @@ If you want to use rails caching of Configurable updates, simply set
100
107
 
101
108
  ```ruby
102
109
  config.use_cache = true
103
- ```
110
+ ```
104
111
  in your `config/application.rb` (or `config/production.rb`)
105
112
 
106
113
  ## Running the Tests ##
@@ -6,6 +6,7 @@ class Configurable < ActiveRecord::Base
6
6
  validates_uniqueness_of :name
7
7
 
8
8
  validate :type_of_value
9
+ before_save :serialize_value
9
10
 
10
11
  def self.defaults
11
12
  @defaults ||= HashWithIndifferentAccess.new(
@@ -29,32 +30,59 @@ class Configurable < ActiveRecord::Base
29
30
  end
30
31
 
31
32
  def self.[](key)
32
- return self.defaults[key][:default] unless table_exists?
33
+ return parse_value key, defaults[key][:default] unless table_exists?
33
34
 
34
- value = if ConfigurableEngine::Engine.config.use_cache
35
- Rails.cache.fetch("configurable_engine:#{key}") {
36
- find_by_name(key).try(:value)
37
- }
38
- else
35
+ val = if ConfigurableEngine::Engine.config.use_cache
36
+ Rails.cache.fetch("configurable_engine:#{key}") {
39
37
  find_by_name(key).try(:value)
40
- end
38
+ }
39
+ else
40
+ find_by_name(key).try(:value)
41
+ end
42
+
43
+ val ||= parse_value key, defaults[key][:default]
44
+ end
45
+
46
+ def value
47
+ self.class.parse_value name, super
48
+ end
41
49
 
42
- value ||= self.defaults[key][:default]
43
- case self.defaults[key][:type]
50
+ def self.parse_value key, value
51
+ case defaults[key][:type]
44
52
  when 'boolean'
45
53
  [true, 1, "1", "t", "true"].include?(value)
46
54
  when 'decimal'
55
+ value ||= 0
47
56
  BigDecimal.new(value.to_s)
48
57
  when 'integer'
49
58
  value.to_i
50
59
  when 'list'
51
- return value if value.is_a?(Array)
52
- value.split("\n").collect{ |v| v.split(',') }
60
+ value ||= []
61
+ if value.is_a? Array
62
+ value
63
+ else
64
+ value.split("\n").collect { |v| v =~ /,/ ? v.split(',') : v }
65
+ end
53
66
  else
54
67
  value
55
68
  end
56
69
  end
57
70
 
71
+ def self.serialized_value key
72
+ value_for_serialization key, self[key]
73
+ end
74
+
75
+ def self.value_for_serialization(key, value)
76
+ if defaults[key][:type] == 'list' && value.is_a?(Array)
77
+ if value.all? {|entry| entry.is_a? Array}
78
+ value = value.collect {|entry| entry.join ','}
79
+ end
80
+ value.join("\n")
81
+ else
82
+ value.to_s
83
+ end.gsub("\r\n","\n")
84
+ end
85
+
58
86
  def self.method_missing(name, *args)
59
87
  name_stripped = name.to_s.sub(/[\?=]$/, '')
60
88
  if self.keys.include?(name_stripped)
@@ -72,16 +100,23 @@ class Configurable < ActiveRecord::Base
72
100
 
73
101
  def type_of_value
74
102
  return unless name
75
- case Configurable.defaults[name][:type]
103
+ valid = case Configurable.defaults[name][:type]
76
104
  when 'boolean'
77
105
  [true, 1, "1", "true", false, 0, "0", "false"].include?(value)
78
106
  when 'decimal'
79
- Float(value) rescue errors.add(:value, I18n.t("activerecord.errors.messages.invalid"))
107
+ BigDecimal(value) rescue false
80
108
  when 'integer'
81
- Integer(value) rescue errors.add(:value, I18n.t("activerecord.errors.messages.invalid"))
109
+ Integer(value) rescue false
82
110
  when 'list'
83
- value.is_a?(Array) rescue errors.add(:value, I18n.t("activerecord.errors.messages.invalid"))
111
+ value.is_a?(Array)
112
+ else
113
+ true
84
114
  end
115
+ errors.add(:value, I18n.t("activerecord.errors.messages.invalid")) unless valid
116
+ end
117
+
118
+ def serialize_value
119
+ self.value = Configurable.value_for_serialization name, value
85
120
  end
86
121
 
87
122
  def invalidate_cache
@@ -13,7 +13,7 @@
13
13
  <%- elsif options[:type] == 'text' -%>
14
14
  <%= text_area_tag key, Configurable.send(key) %>
15
15
  <%- elsif options[:type] == 'list' -%>
16
- <%= text_area_tag key, Configurable.send(key).collect{ |a| a.join(",")}.join("\n") %>
16
+ <%= text_area_tag key, Configurable.serialized_value(key) -%>
17
17
  <%- else -%>
18
18
  <%= text_field_tag key, Configurable.send(key) %>
19
19
  <%- end -%>
@@ -5,12 +5,21 @@ module ConfigurableEngine
5
5
  end
6
6
 
7
7
  def update
8
- Configurable.keys.each do |key|
9
- configurable = Configurable.find_by_name(key) ||
8
+ failures = Configurable
9
+ .keys.map do |key|
10
+ Configurable.find_by_name(key) ||
10
11
  Configurable.create {|c| c.name = key}
11
- configurable.update_attribute(:value,params[key])
12
+ end.reject do |configurable|
13
+ configurable.value = params[configurable.name]
14
+ configurable.save
15
+ end
16
+
17
+ if failures.empty?
18
+ redirect_to admin_configurable_path, :notice => "Changes successfully updated"
19
+ else
20
+ flash[:error] = failures.flat_map(&:errors).flat_map(&:full_messages).join(',')
21
+ redirect_to admin_configurable_path
12
22
  end
13
- redirect_to admin_configurable_path, :notice => "Changes successfully updated"
14
23
  end
15
24
  end
16
25
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigurableEngine
2
- VERSION = '0.3.3'
2
+ VERSION = '0.4.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configurable_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Campbell