Permalink
Browse files

Moved almost all to gem and engine.

  • Loading branch information...
1 parent 021b6d8 commit 3e5e2cac3ffa2c1880aa9a88031110ab02a25d9f @leeatchison committed Nov 23, 2011
View
@@ -217,3 +217,9 @@ Suggestions for this gem can be sent to me.
# Testing Soft Launch
Simply run "rake test". The gem uses rspec for all of it's test suites.
+
+
+
+
+# XXXX
+Talk about using "main_app.xxx_path" on links in the application.html.erb template, so that this template can be used for the engine...
@@ -0,0 +1,24 @@
+class SoftLaunch
+ class SoftLaunchController < ApplicationController
+ def show
+ # render :layout => "soft_launch/application"
+ @softlaunch=SoftLaunch.find_by_usercode params[:id]
+ render :layout => SoftLaunch.engine_layout
+ end
+ def update
+ @softlaunch=SoftLaunch.find_by_usercode params[:id]
+ if params[:sl_enable].to_i!=0
+ @softlaunch.enable=true
+ flash[:info]="Enabled feature #{@softlaunch.name}"
+ else
+ @softlaunch.enable=false
+ flash[:info]="Disabled feature #{@softlaunch.name}"
+ end
+ redirect_to soft_launch_path id: @softlaunch.usercode
+ end
+
+ def soft_launch_controller?
+ true
+ end
+ end
+end
@@ -0,0 +1,31 @@
+<h1>Soft Launch: <%=@softlaunch.name%></h1>
+<table class="softlaunchstatus">
+ <tr>
+ <td class="label">Public Status:</td>
+ <td class="value"><%=@softlaunch.status_string%></td>
+ </tr>
+ <tr>
+ <td class="label">This Browser Status:</td>
+ <%if @softlaunch.launched?%>
+ <td class="value">
+ Enabled
+ </td>
+ <td class="value">
+ <%=form_for @softlaunch.usercode,:method=>:put do |f|%>
+ <%=hidden_field_tag :sl_enable,0%>
+ <%=f.submit "Disable"%>
+ <%end%>
+ </td>
+ <%else%>
+ <td class="value">
+ Disabled
+ </td>
+ <td class="value">
+ <%=form_for @softlaunch.usercode,:method=>:put do |f|%>
+ <%=hidden_field_tag :sl_enable,1%>
+ <%=f.submit "Enable"%>
+ <%end%>
+ </td>
+ <%end%>
+ </tr>
+</table>
View
@@ -0,0 +1,3 @@
+SoftLaunch::Engine.routes.draw do
+ resources :soft_launch, only: [:show,:update],path: ""
+end
View
@@ -1,5 +1,4 @@
require "softlaunch/version"
-
-module Softlaunch
- # Your code goes here...
-end
+require "softlaunch/model"
+require "softlaunch/application_controller"
+require "softlaunch/engine"
@@ -0,0 +1,15 @@
+module SoftLaunchApplicationController
+ def launched? ident
+ sl=SoftLaunch.find ident
+ return false if sl.nil?
+ sl.launched?
+ end
+ def softlaunch ident
+ sl=SoftLaunch.find ident
+ return false if sl.nil?
+ sl.launched?
+ end
+ def setup_soft_launch
+ SoftLaunch.cookies=cookies
+ end
+end
View
@@ -0,0 +1,5 @@
+class SoftLaunch
+ class Engine < Rails::Engine
+ isolate_namespace SoftLaunch
+ end
+end
View
@@ -0,0 +1,112 @@
+require 'yaml'
+class SoftLaunch
+ attr_reader :id,:name,:usercode
+
+ def initialize feature_id
+ info=SoftLaunch.config[feature_id.to_s]
+ raise SoftLaunch::InvalidFeature if info.nil?
+ @id=feature_id
+ @name=info["name"]
+ @status=info["status"].to_sym
+ @usercode=info["usercode"]
+ end
+
+ def launched?
+ return true if @status==:enabled
+ return false if @status==:disabled
+ return false if !user_specific?
+ raise SoftLaunch::CookiesNotConfigured if SoftLaunch.cookies.nil?
+ # User Status...(force true and false, even if 1, 0, nil, etc.)
+ return true if SoftLaunch.cookies[cookie_name]
+ return false
+ end
+
+ def enabled?
+ @status==:enabled
+ end
+
+ def disabled?
+ @status==:disabled
+ end
+
+ def user_specific?
+ @status==:user
+ end
+
+ def enable= status
+ if status
+ SoftLaunch.cookies[cookie_name]=true
+ else
+ SoftLaunch.cookies.delete cookie_name
+ end
+ end
+
+ def status_string
+ case @status
+ when :disabled then "Disabled"
+ when :enabled then "Enabled"
+ when :user then "Per User"
+ else
+ "???#{@status}???"
+ end
+ end
+
+ class << self
+ attr_accessor :cookies, :engine_layout
+ def all
+ ret=[]
+ config.each do |id,data|
+ ret<<SoftLaunch.new(id)
+ end
+ ret
+ end
+ def find feature
+ config.each do |id,data|
+ return SoftLaunch.new id if id.to_s==feature.to_s
+ end
+ return nil
+ end
+ def find_by_usercode usercode
+ config.each do |id,data|
+ return SoftLaunch.new id if data["usercode"] and data["usercode"].to_s == usercode.to_s
+ end
+ return nil
+ end
+
+
+
+ def reset
+ @config_file=nil
+ @config=nil
+ end
+ def config_file= config_file # Where is the config file located?
+ raise FileAlreadyInUse if @config
+ @config_file=config_file
+ end
+ def config_file
+ @config_file||=Rails.root.join "config/softlaunch.yml"
+ end
+ def config
+ return @config if @config
+ raw_config = File.read(config_file)
+ if raw_config.nil?
+ puts "No configuration file provided"
+ logger.error "No configuration file provided"
+ raise InvalidConfiguration
+ end
+ @config = YAML::load(raw_config)[ENV["RAILS_ENV"]||"development"]
+ @config
+ end
+
+ class FileAlreadyInUse<Exception;end
+ class InvalidFeature<Exception;end
+ class InvalidConfiguration<Exception;end
+ class CookiesNotConfigured<Exception;end
+ end
+
+ private
+ def cookie_name
+ "soft_launch_#{id}".to_sym
+ end
+
+end
@@ -0,0 +1,11 @@
+test:
+ feature1:
+ name: My New Feature 1
+ status: enabled
+ feature2:
+ name: My New Feature 2
+ status: disabled
+ feature3:
+ name: My New Feature 3
+ status: user
+ usercode: thisisasampleusercode
View
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe "Soft Launch Asker" do
+ before :each do
+ SoftLaunch.reset
+ SoftLaunch.config_file=Rails.root.join "spec/config_templates/basic.yml"
+ end
+ it "should report launched if feature is enabled" do
+ launched?(:feature1).should be_true
+ end
+ it "should report not launched if feature is disabled" do
+ launched?(:feature2).should be_false
+ end
+ it "should report launched if feature is user and there is a proper cookie" do
+ SoftLaunch.cookies={:soft_launch_feature3=>true}
+ launched?(:feature3).should be_true
+ end
+ it "should report not launched if feature is user and there is no proper cookie" do
+ SoftLaunch.cookies={}
+ launched?(:feature1).should be_false
+ end
+end
View
@@ -0,0 +1,83 @@
+require 'spec_helper'
+
+describe SoftLaunch do
+ before :each do
+ SoftLaunch.reset
+ SoftLaunch.config_file="spec/config_templates/basic.yml"
+ end
+ it "reads configuration from a specified configuration file" do
+ SoftLaunch.config_file.should eq "spec/config_templates/basic.yml"
+ end
+ it "allows setting features as enabled" do
+ sl=SoftLaunch.find "feature1"
+ sl.name.should eq "My New Feature 1"
+ sl.enabled?.should be_true
+ sl.disabled?.should be_false
+ sl.user_specific?.should be_false
+ end
+ it "allows setting features as disabled" do
+ sl=SoftLaunch.find "feature2"
+ sl.name.should eq "My New Feature 2"
+ sl.enabled?.should be_false
+ sl.disabled?.should be_true
+ sl.user_specific?.should be_false
+ end
+ it "allows setting features as user enablable" do
+ sl=SoftLaunch.find "feature3"
+ sl.name.should eq "My New Feature 3"
+ sl.enabled?.should be_false
+ sl.disabled?.should be_false
+ sl.user_specific?.should be_true
+ end
+ it "should report launched if feature is enabled" do
+ sl=SoftLaunch.find "feature1"
+ sl.launched?.should be_true
+ end
+ it "should report not launched if feature is disabled" do
+ sl=SoftLaunch.find "feature2"
+ sl.launched?.should be_false
+ end
+ it "should report launched if feature is user and there is a proper cookie" do
+ SoftLaunch.cookies={:soft_launch_feature3=>true}
+ sl=SoftLaunch.find "feature3"
+ sl.launched?.should be_true
+ end
+ it "should report not launched if feature is user and there is no proper cookie" do
+ SoftLaunch.cookies={}
+ sl=SoftLaunch.find "feature3"
+ sl.launched?.should be_false
+ end
+ it "should be able to get a list of all features" do
+ list=SoftLaunch.all
+ list.size.should eq 3
+ list[0].name.should eq "My New Feature 1"
+ list[1].name.should eq "My New Feature 2"
+ list[2].name.should eq "My New Feature 3"
+ end
+ it "should be able to look up a feature by feature name" do
+ feature=SoftLaunch.find "feature2"
+ feature.name.should eq "My New Feature 2"
+ end
+ it "should be able to look up a feature by user code" do
+ feature=SoftLaunch.find_by_usercode "thisisasampleusercode"
+ feature.name.should eq "My New Feature 3"
+ end
+
+ it "should be able to set a cookie to enable a feature" do
+ cookies={}
+ cookies[:soft_launch_feature3].should be_nil
+ SoftLaunch.cookies=cookies
+ sl=SoftLaunch.find "feature3"
+ sl.enable=true
+ cookies[:soft_launch_feature3].should be_true
+ end
+ it "should be able to clear a cookie to disable a feature" do
+ cookies={:soft_launch_feature3=>true}
+ cookies[:soft_launch_feature3].should be_true
+ SoftLaunch.cookies=cookies
+ sl=SoftLaunch.find "feature3"
+ sl.enable=false
+ cookies[:soft_launch_feature3].should be_nil
+ end
+
+end
View
@@ -1 +1,2 @@
-require 'softlaunch'
+require 'softlaunch'
+ENV["RAILS_ENV"]||="test"

0 comments on commit 3e5e2ca

Please sign in to comment.