behavior 0.1.0

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.
@@ -0,0 +1,16 @@
1
+ class CreateBehaviorConfigs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :behavior_configs, :force => true do |t|
4
+ t.string :key
5
+ t.text :value
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :behavior_configs, :key
10
+ end
11
+
12
+ def self.down
13
+ remove_index :behavior_configs, :key
14
+ drop_table :behavior_configs
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ # This file controls what config variables you want to be able to allow your users
2
+ # to set, as well as those you'll be able to access from within the application.
3
+ #
4
+ # If you want to be able to access a string config[:site_title], for example:
5
+ #
6
+ # site_title:
7
+ # name: Site Title
8
+ # type: string
9
+ # default: My Site
10
+ #
11
+ # 'name' is the name that appears in the edit form
12
+ #
13
+ # 'type' can be 'string' for a text field, 'password' for a password field or 'text' for a text area
14
+ # 'type' defaults to 'string'
15
+ #
16
+ # 'default' is the default value to use if there's no entry in the database. Otherwise, nil will be returned
17
+ #
18
+ # Some Examples:
19
+ #
20
+ # site_title:
21
+ # name: Site Title
22
+ # default: My Site
23
+ # type: string
24
+ #
25
+ # site_description:
26
+ # name: Description for Google
27
+ # default: Lots of Awesomeness Here
28
+ # type: text
29
+ #
30
+ # secret:
31
+ # name: Secret Password for Accessing Secret Areas
32
+ # default: secret
33
+ # type: password
File without changes
data/lib/behavior.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'active_support'
2
+ module Behavior
3
+ def self.included(base)
4
+ base.class_eval do
5
+ def config
6
+ Behavior::Base.new
7
+ end
8
+ helper_method :config if respond_to?(:helper_method)
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def config
14
+ Behavior::Base.new
15
+ end
16
+ end
17
+
18
+ class Base
19
+ def meta
20
+ HashWithIndifferentAccess.new(YAML.load_file(Behavior::Settings.config_file))
21
+ end
22
+
23
+ def [](key)
24
+ begin
25
+ BehaviorConfig.find_by_key(key.to_s).value
26
+ rescue NoMethodError
27
+ meta[key][:default]
28
+ end
29
+ end
30
+
31
+ def all
32
+ meta.keys
33
+ end
34
+
35
+ def update(attrs = {})
36
+ attrs.each do |key,value|
37
+ result = BehaviorConfig.find_or_create_by_key(key.to_s)
38
+ result.update_attribute(:value, value) if result
39
+ end
40
+ end
41
+ end
42
+
43
+ class Settings
44
+ class << self
45
+ def config_file
46
+ @config_file ||= "#{Rails.root}/config/behavior.yml"
47
+ end
48
+
49
+ def config_file=(file)
50
+ @config_file = file
51
+ end
52
+
53
+ def layout
54
+ @layout ||= 'admin'
55
+ end
56
+
57
+ def layout=(layout_choice)
58
+ @layout = layout_choice
59
+ end
60
+
61
+ def before_filters
62
+ @before_filters ||= []
63
+ end
64
+
65
+ def before_filters=(filters)
66
+ @before_filters = filters
67
+ end
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ ActionController::Base.send(:include, Behavior)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'behavior'
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'rails_generator'
3
+ require 'rails_generator/scripts/generate'
4
+
5
+ describe BehaviorGenerator do
6
+
7
+ before do
8
+ @dirs = {}
9
+ @dirs['migration'] = fake_rails_root + '/db/migrate'
10
+ @dirs['config'] = fake_rails_root + '/config'
11
+ @dirs.each do |key, dir|
12
+ FileUtils.mkdir_p(dir)
13
+ end
14
+ @original_files = {}
15
+ @original_files['migration'] = file_list(@dirs['migration'])
16
+ @original_files['config'] = file_list(@dirs['config'])
17
+ Rails::Generator::Scripts::Generate.new.run(["behavior"], :destination => fake_rails_root, :backtrace => true)
18
+ end
19
+
20
+ it "should create the migration file" do
21
+ new_migration_file = (file_list(@dirs['migration']) - @original_files['migration']).first
22
+ "20091210164854_create_behavior_configs.rb".should == File.basename(new_migration_file)
23
+ end
24
+
25
+ it "should create the config file" do
26
+ new_config_file = (file_list(@dirs['config']) - @original_files['config']).first
27
+ "behavior.yml".should == File.basename(new_config_file)
28
+ end
29
+
30
+ after do
31
+ FileUtils.rm_r(fake_rails_root)
32
+ end
33
+
34
+
35
+ def fake_rails_root
36
+ File.join(File.dirname(__FILE__), 'spec', 'rails_root')
37
+ end
38
+
39
+ def file_list(dir)
40
+ Dir.glob(File.join(dir, "*"))
41
+ end
42
+
43
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Behavior do
4
+
5
+ include Behavior
6
+
7
+ before do
8
+ Behavior::Settings.config_file = 'config/behavior.yml'
9
+ BehaviorConfig.make(:key => 'email_address', :value => 'paul@rslw.com')
10
+ end
11
+
12
+ describe ".config" do
13
+ it "should wonder why" do
14
+ config.should be_a_kind_of(Behavior::Base)
15
+ end
16
+
17
+ it "should allow accessing the config" do
18
+ config.meta[:email_address][:name].should == "Email Address"
19
+ end
20
+
21
+ it "should give me all the keys" do
22
+ config.all.should == ["email_name", "email_address", "description", "password"]
23
+ end
24
+
25
+ it "should get me my value" do
26
+ config[:email_address].should == "paul@rslw.com"
27
+ end
28
+
29
+ it "should have a nice default" do
30
+ config[:email_name].should == "Site Administrator"
31
+ end
32
+ end
33
+
34
+ describe "#update" do
35
+ it "should let me update the config" do
36
+ config.update(:email_address => 'joe@putplace.com')
37
+ BehaviorConfig.find_by_key('email_address').value.should == "joe@putplace.com"
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,8 @@
1
+ require 'machinist/active_record'
2
+ require 'faker'
3
+ require 'sham'
4
+
5
+ BehaviorConfig.blueprint do
6
+ key 'test key'
7
+ value 'test value'
8
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,21 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :database: spec/behavior.sqlite.db
4
+
5
+ sqlite3:
6
+ :adapter: sqlite3
7
+ :database: spec/behavior.sqlite3.db
8
+
9
+ postgresql:
10
+ :adapter: postgresql
11
+ :username: postgres
12
+ :password: postgres
13
+ :database: behavior_test
14
+ :min_messages: ERROR
15
+
16
+ mysql:
17
+ :adapter: mysql
18
+ :host: localhost
19
+ :username: root
20
+ :password: password
21
+ :database: behavior_test