self-control 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -0,0 +1,46 @@
1
+ module SelfControl
2
+ class Action
3
+ attr_accessor :name, :goto, :for, :options
4
+
5
+ def initialize(name, options={})
6
+ @name = name
7
+ @goto = options.delete(:goto)
8
+ @hash = options.delete(:hash)
9
+ @arguments = options.delete(:the) || options.delete(:for) || options.delete(:with)
10
+ end
11
+
12
+ def url_from(model, env)
13
+ goto = in_context(@goto, model, env)
14
+ hash = in_context(@hash, model, env)
15
+ args = in_context(@arguments, model, env)
16
+
17
+ url = if goto.is_a?(String) then goto
18
+ elsif goto.is_a?(Symbol)
19
+ env.send(goto, args)
20
+ else
21
+ env.send(:url_for, (goto || args))
22
+ end
23
+
24
+ hash ? "#{url}##{hash}" : url
25
+ end
26
+
27
+ def in_context(option, model, env)
28
+ case option
29
+ when String then option
30
+ when Symbol
31
+ if model.respond_to?(option) then model.send(option)
32
+ elsif option.to_s.starts_with?('@')
33
+ env.instance_variable_get(option)
34
+ else
35
+ option
36
+ end
37
+ when Array
38
+ option.map { |opt| in_context(opt, model, env) }
39
+ when Hash
40
+ Hash[option.map { |k,v| [k.to_sym, in_context(v, model, env)] }]
41
+ when Proc
42
+ option.call(*[model,env].take(option.arity >= 0 ? option.arity : 0))
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,26 @@
1
+ module SelfControl
2
+ class Builder
3
+ attr_accessor :klass, :actions, :flows, :state_column
4
+
5
+ def initialize(klass, state_column=false, &block)
6
+ @state_column = (state_column ? state_column.to_sym : false)
7
+ @klass = klass
8
+ @actions = {}
9
+ @flows = {}
10
+ instance_eval(&block)
11
+ end
12
+
13
+ private
14
+
15
+ def to(action,options={})
16
+ action = action.to_sym
17
+ @actions[action] = SelfControl::Action.new(action, options)
18
+ end
19
+
20
+ def steps(options={}, &block)
21
+ state = (options.delete(:when) || :default).to_sym
22
+ @flows[state] = SelfControl::FlowBuilder.new(state, &block)
23
+ end
24
+ end
25
+ end
26
+
@@ -1,8 +1,9 @@
1
1
  module SelfControl
2
- module ControllerAdditions
2
+ module ControllerExtensions
3
3
  extend ActiveSupport::Concern
4
4
 
5
- module InstanceMethods
5
+ module InstanceMethods
6
+
6
7
  def steps_list
7
8
  @selfcontrol_steps = valid_selfcontrol? ? selfcontrol.steps_for(selfcontrol_actor) : []
8
9
  respond_with(@selfcontrol_steps)
@@ -75,8 +76,7 @@ module SelfControl
75
76
  unless allow_selfcontrol_step?
76
77
  raise SelfControl::AccessDenied.new(nil, selfcontrol_actor, params[:step], params[:choose])
77
78
  end
78
- end
79
-
79
+ end
80
80
  end
81
81
  end
82
82
  end
@@ -0,0 +1,11 @@
1
+ module SelfControl
2
+ module Helper
3
+ def step_url_for(resource, action_name, options={})
4
+ return unless resource
5
+ return unless builder = resource.selfcontrol_builder
6
+ action_name = action_name.to_sym
7
+ action = builder.actions[action_name] || SelfControl::Action.new(action_name, options)
8
+ action.url_from(resource,self).html_safe
9
+ end
10
+ end
11
+ end
@@ -1,8 +1,15 @@
1
- require 'self-control/route_helpers'
2
- require 'self-control/controller_additions'
1
+ require 'self-control/helper'
3
2
  require 'self-control/step_adapter'
3
+ require 'self-control/route_helpers'
4
+ require 'self-control/controller_extensions'
4
5
 
5
6
  module SelfControl
6
7
  class Railtie < Rails::Railtie
8
+ initializer 'selfcontrol.app_controller' do |app|
9
+ ActionController::Base.helper(SelfControl::Helper)
10
+ ActiveSupport.on_load(:application_controller) do
11
+ include SelfControl::ControllerExtensions
12
+ end
13
+ end
7
14
  end
8
15
  end
@@ -33,9 +33,17 @@ module SelfControl
33
33
  @builder.actor.to_sym
34
34
  end
35
35
 
36
+ def model_id
37
+ @model.id if @model.respond_to?(:id)
38
+ end
39
+
36
40
  def model_name
37
41
  ActiveModel::Naming.singular(@model) if defined?(ActiveModel::Naming)
38
42
  end
43
+
44
+ def model_collection
45
+ ActiveModel::Naming.plural(@model) if defined?(ActiveModel::Naming)
46
+ end
39
47
 
40
48
  def actor
41
49
  return if actor_method.nil?
data/lib/self-control.rb CHANGED
@@ -5,46 +5,41 @@ module SelfControl
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  module ClassMethods
8
- attr_reader :selfcontrol_list
9
- attr_writer :selfcontrol_column
8
+ attr_reader :selfcontrol_builder
10
9
 
11
- def selfcontrol(state=nil, options={}, &block)
12
- state = (state || options[:when]).to_sym
13
- @selfcontrol_list ||= {}
14
- @selfcontrol_list[state] = SelfControl::FlowBuilder.new(state, &block)
10
+ def selfcontrol(options={}, &block)
11
+ state_column = options.delete(:for)
12
+ @selfcontrol_builder = SelfControl::Builder.new(self, state_column, &block)
15
13
  end
16
14
 
17
- alias :control_when :selfcontrol
18
-
19
- def selfcontrol_column
20
- @selfcontrol_column ||= 'state'
21
- end
15
+ alias :flowcontrol :selfcontrol
22
16
  end
23
17
 
24
- module InstanceMethods
25
- def selfcontrol_list
26
- self.class.selfcontrol_list
27
- end
28
-
29
- def selfcontrol_column
30
- self.class.selfcontrol_column
18
+ module InstanceMethods
19
+ def selfcontrol_builder
20
+ self.class.selfcontrol_builder
31
21
  end
32
22
 
33
- def current_selfcontrol_state
34
- (self.respond_to?(selfcontrol_column) ? self.send(selfcontrol_column) : :default).to_sym
35
- end
36
-
37
- def selfcontrol(state=current_selfcontrol_state)
38
- return unless builder = self.selfcontrol_list[state]
23
+ def selfcontrol(state=nil)
24
+ return false unless selfcontrol_builder.is_a?(SelfControl::Builder)
25
+
26
+ state ||= :default unless state_column = selfcontrol_builder.state_column
27
+ state ||= (self.respond_to?(state_column) ? self.send(state_column) : :default).to_sym
28
+
39
29
  @selfcontrol_flows ||= {}
40
- @selfcontrol_flows[state] ||= SelfControl::Flow.new(builder,self)
41
- end
30
+ return @selfcontrol_flows[state] if @selfcontrol_flows[state].is_a?(SelfControl::Flow)
31
+
32
+ return false unless builder = selfcontrol_builder.flows[state]
33
+ @selfcontrol_flows[state] = SelfControl::Flow.new(builder, self)
34
+ end
42
35
  end
43
36
 
37
+ autoload :Builder, 'self-control/builder'
44
38
  autoload :FlowBuilder, 'self-control/flow_builder'
45
39
  autoload :StepBuilder, 'self-control/step_builder'
46
40
  autoload :Flow, 'self-control/flow'
47
41
  autoload :Step, 'self-control/step'
42
+ autoload :Action, 'self-control/action'
48
43
  end
49
44
 
50
45
  require 'self-control/railtie' if defined?(Rails)
data/self-control.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{self-control}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hexorx"]
12
- s.date = %q{2011-06-20}
12
+ s.date = %q{2011-06-22}
13
13
  s.description = %q{State machines are awesome but sometimes you need a little more. Like who should do what in order for it to move on? How many steps are left? How can I restfully trigger the next event? Self Control adds route helpers, controller actions and a dsl to turn your existing state machines into full workflows. It is designed to use rails 3 with ActiveModel and should work with any state machine with just a few extra methods.}
14
14
  s.email = %q{hexorx@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -23,10 +23,13 @@ Gem::Specification.new do |s|
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "lib/self-control.rb",
26
- "lib/self-control/controller_additions.rb",
26
+ "lib/self-control/action.rb",
27
+ "lib/self-control/builder.rb",
28
+ "lib/self-control/controller_extensions.rb",
27
29
  "lib/self-control/exceptions.rb",
28
30
  "lib/self-control/flow.rb",
29
31
  "lib/self-control/flow_builder.rb",
32
+ "lib/self-control/helper.rb",
30
33
  "lib/self-control/railtie.rb",
31
34
  "lib/self-control/route_helpers.rb",
32
35
  "lib/self-control/step.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: self-control
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - hexorx
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-20 00:00:00 -06:00
18
+ date: 2011-06-22 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -62,10 +62,13 @@ files:
62
62
  - Rakefile
63
63
  - VERSION
64
64
  - lib/self-control.rb
65
- - lib/self-control/controller_additions.rb
65
+ - lib/self-control/action.rb
66
+ - lib/self-control/builder.rb
67
+ - lib/self-control/controller_extensions.rb
66
68
  - lib/self-control/exceptions.rb
67
69
  - lib/self-control/flow.rb
68
70
  - lib/self-control/flow_builder.rb
71
+ - lib/self-control/helper.rb
69
72
  - lib/self-control/railtie.rb
70
73
  - lib/self-control/route_helpers.rb
71
74
  - lib/self-control/step.rb