aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-31 14:32:26 +0000
committerPratik Naik <pratiknaik@gmail.com>2010-01-31 14:32:26 +0000
commited60021f39f7913537dcad181e4061e423dc6c3d (patch)
tree86532c43aabe9125422f84b48a899f2672900bf5 /activemodel/lib/active_model
parentc6af337d2d6c86792cdc8132224ebe9294d35774 (diff)
parentb3a028259f373fd58fea2171a1e9e8b2fe3e253a (diff)
downloadrails-ed60021f39f7913537dcad181e4061e423dc6c3d.tar.gz
rails-ed60021f39f7913537dcad181e4061e423dc6c3d.tar.bz2
rails-ed60021f39f7913537dcad181e4061e423dc6c3d.zip
Merge remote branch 'mainstream/master'
Conflicts: activemodel/lib/active_model/state_machine.rb
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/errors.rb21
-rw-r--r--activemodel/lib/active_model/state_machine.rb219
-rw-r--r--activemodel/lib/active_model/state_machine/event.rb62
-rw-r--r--activemodel/lib/active_model/state_machine/machine.rb75
-rw-r--r--activemodel/lib/active_model/state_machine/state.rb47
-rw-r--r--activemodel/lib/active_model/state_machine/state_transition.rb40
6 files changed, 14 insertions, 450 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 76e6ad93a7..f1d74db5f3 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -242,28 +242,35 @@ module ActiveModel
# <li><tt>activemodel.errors.models.admin.blank</tt></li>
# <li><tt>activemodel.errors.models.user.attributes.title.blank</tt></li>
# <li><tt>activemodel.errors.models.user.blank</tt></li>
- # <li><tt>activemodel.errors.messages.blank</tt></li>
# <li>any default you provided through the +options+ hash (in the activemodel.errors scope)</li>
+ # <li><tt>activemodel.errors.messages.blank</tt></li>
+ # <li><tt>errors.attributes.title.blank</tt></li>
+ # <li><tt>errors.messages.blank</tt></li>
# </ol>
def generate_message(attribute, message = :invalid, options = {})
message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
defaults = @base.class.lookup_ancestors.map do |klass|
- [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}",
- :"models.#{klass.name.underscore}.#{message}" ]
+ [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{message}",
+ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{message}" ]
end
defaults << options.delete(:default)
- defaults = defaults.compact.flatten << :"messages.#{message}"
+ defaults << :"#{@base.class.i18n_scope}.errors.messages.#{message}"
+ defaults << :"errors.attributes.#{attribute}.#{message}"
+ defaults << :"errors.messages.#{message}"
+
+ defaults.compact!
+ defaults.flatten!
key = defaults.shift
value = @base.send(:read_attribute_for_validation, attribute)
- options = { :default => defaults,
+ options = {
+ :default => defaults,
:model => @base.class.model_name.human,
:attribute => @base.class.human_attribute_name(attribute),
- :value => value,
- :scope => [:errors]
+ :value => value
}.merge(options)
I18n.translate(key, options)
diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb
deleted file mode 100644
index 64b91c1659..0000000000
--- a/activemodel/lib/active_model/state_machine.rb
+++ /dev/null
@@ -1,219 +0,0 @@
-module ActiveModel
-
- # ActiveModel::StateMachine provides methods that turn your object into a
- # finite state machine, able to move from one state to another.
- #
- # A minimal implementation could be:
- #
- # class EmailMessage
- # include ActiveModel::StateMachine
- #
- # state_machine do
- # state :unread
- # state :read
- # end
- #
- # event :open_email do
- # transitions :to => :read, :from => :unread
- # end
- # end
- #
- # === Examples
- #
- # class TrafficLight
- # include ActiveModel::StateMachine
- #
- # attr_reader :runners_caught
- #
- # def initialize
- # @runners_caught = 0
- # end
- #
- # state_machine do
- # state :red
- # state :green
- # state :yellow
- # state :blink
- #
- # event :change_color do
- # transitions :to => :red, :from => [:yellow],
- # :on_transition => :catch_runners
- # transitions :to => :green, :from => [:red]
- # transitions :to => :yellow, :from => [:green]
- # end
- #
- # event :defect do
- # transitions :to => :blink, :from => [:yellow, :red, :green]
- # end
- #
- # event :repair do
- # transitions :to => :red, :from => [:blink]
- # end
- # end
- #
- # def catch_runners
- # @runners_caught += 1
- # end
- # end
- #
- # light = TrafficLight.new
- # light.current_state # => :red
- # light.change_color! # => true
- # light.current_state # => :green
- # light.green? # => true
- # light.change_color! # => true
- # light.current_state # => :yellow
- # light.red? # => false
- # light.change_color! # => true
- # light.runners_caught # => 1
- #
- # * The initial state for TrafficLight is red which is the first state defined.
- #
- # TrafficLight.state_machine.initial_state # => :red
- #
- # * Call an event to transition a state machine, e.g. <tt>change_color!</tt>.
- # You can call the event with or without the exclamation mark, however, the common Ruby
- # idiom is to name methods that directly change the state of the receivier with
- # an exclamation mark, so <tt>change_color!</tt> is preferred over <tt>change_color</tt>.
- #
- # light.current_state #=> :green
- # light.change_color! #=> true
- # light.current_state #=> :yellow
- #
- # * On a succesful transition to red (from yellow), the local +catch_runners+
- # method is executed
- #
- # light.current_state #=> :red
- # light.change_color! #=> true
- # light.runners_caught #=> 1
- #
- # * The object acts differently depending on its current state, for instance,
- # the change_color! method has a different action depending on the current
- # color of the light
- #
- # light.change_color! #=> true
- # light.current_state #=> :red
- # light.change_color! #=> true
- # light.current_state #=> :green
- #
- # * Get the possible events for a state
- #
- # TrafficLight.state_machine.events_for(:red) # => [:change_color, :defect]
- # TrafficLight.state_machine.events_for(:blink) # => [:repair]
- #
- # The StateMachine also supports the following features :
- #
- # * Success callbacks on event transition
- #
- # event :sample, :success => :we_win do
- # ...
- # end
- #
- # * Enter and exit callbacks par state
- #
- # state :open, :enter => [:alert_twitter, :send_emails], :exit => :alert_twitter
- #
- # * Guards on transition
- #
- # event :close do
- # # You may only close the store if the safe is locked!!
- # transitions :to => :closed, :from => :open, :guard => :safe_locked?
- # end
- #
- # * Setting the initial state
- #
- # state_machine :initial => :yellow do
- # ...
- # end
- #
- # * Named the state machine, to have more than one
- #
- # class Stated
- # include ActiveModel::StateMachine
- #
- # strate_machine :name => :ontest do
- # end
- #
- # state_machine do
- # end
- # end
- #
- # # Get the state of the <tt>:ontest</tt> state machine
- # stat.current_state(:ontest)
- # # Get the initial state
- # Stated.state_machine(:ontest).initial_state
- #
- # * Changing the state
- #
- # stat.current_state(:default, :astate) # => :astate
- # # But you must give the name of the state machine, here <tt>:default</tt>
- #
- module StateMachine
- autoload :Event, 'active_model/state_machine/event'
- autoload :Machine, 'active_model/state_machine/machine'
- autoload :State, 'active_model/state_machine/state'
- autoload :StateTransition, 'active_model/state_machine/state_transition'
-
- extend ActiveSupport::Concern
-
- class InvalidTransition < Exception
- end
-
- module ClassMethods
- def inherited(klass)
- super
- klass.state_machines = state_machines
- end
-
- def state_machines
- @state_machines ||= {}
- end
-
- def state_machines=(value)
- @state_machines = value ? value.dup : nil
- end
-
- def state_machine(name = nil, options = {}, &block)
- if name.is_a?(Hash)
- options = name
- name = nil
- end
- name ||= :default
- state_machines[name] ||= Machine.new(self, name)
- block ? state_machines[name].update(options, &block) : state_machines[name]
- end
-
- def define_state_query_method(state_name)
- name = "#{state_name}?"
- undef_method(name) if method_defined?(name)
- class_eval "def #{name}; current_state.to_s == %(#{state_name}) end"
- end
- end
-
- def current_state(name = nil, new_state = nil, persist = false)
- sm = self.class.state_machine(name)
- ivar = sm.current_state_variable
- if name && new_state
- if persist && respond_to?(:write_state)
- write_state(sm, new_state)
- end
-
- if respond_to?(:write_state_without_persistence)
- write_state_without_persistence(sm, new_state)
- end
-
- instance_variable_set(ivar, new_state)
- else
- instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar)
- value = instance_variable_get(ivar)
- return value if value
-
- if respond_to?(:read_state)
- value = instance_variable_set(ivar, read_state(sm))
- end
-
- value || sm.initial_state
- end
- end
- end
-end
diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb
deleted file mode 100644
index 30e9601dc2..0000000000
--- a/activemodel/lib/active_model/state_machine/event.rb
+++ /dev/null
@@ -1,62 +0,0 @@
-module ActiveModel
- module StateMachine
- class Event
- attr_reader :name, :success
-
- def initialize(machine, name, options = {}, &block)
- @machine, @name, @transitions = machine, name, []
- if machine
- machine.klass.send(:define_method, "#{name}!") do |*args|
- machine.fire_event(name, self, true, *args)
- end
-
- machine.klass.send(:define_method, name.to_s) do |*args|
- machine.fire_event(name, self, false, *args)
- end
- end
- update(options, &block)
- end
-
- def fire(obj, to_state = nil, *args)
- transitions = @transitions.select { |t| t.from == obj.current_state(@machine ? @machine.name : nil) }
- raise InvalidTransition if transitions.size == 0
-
- next_state = nil
- transitions.each do |transition|
- next if to_state && !Array(transition.to).include?(to_state)
- if transition.perform(obj)
- next_state = to_state || Array(transition.to).first
- transition.execute(obj, *args)
- break
- end
- end
- next_state
- end
-
- def transitions_from_state?(state)
- @transitions.any? { |t| t.from? state }
- end
-
- def ==(event)
- if event.is_a? Symbol
- name == event
- else
- name == event.name
- end
- end
-
- def update(options = {}, &block)
- if options.key?(:success) then @success = options[:success] end
- if block then instance_eval(&block) end
- self
- end
-
- private
- def transitions(trans_opts)
- Array(trans_opts[:from]).each do |s|
- @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym}))
- end
- end
- end
- end
-end
diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb
deleted file mode 100644
index 777531213e..0000000000
--- a/activemodel/lib/active_model/state_machine/machine.rb
+++ /dev/null
@@ -1,75 +0,0 @@
-module ActiveModel
- module StateMachine
- class Machine
- attr_writer :initial_state
- attr_accessor :states, :events, :state_index
- attr_reader :klass, :name
-
- def initialize(klass, name, options = {}, &block)
- @klass, @name, @states, @state_index, @events = klass, name, [], {}, {}
- update(options, &block)
- end
-
- def initial_state
- @initial_state ||= (states.first ? states.first.name : nil)
- end
-
- def update(options = {}, &block)
- if options.key?(:initial) then @initial_state = options[:initial] end
- if block then instance_eval(&block) end
- self
- end
-
- def fire_event(event, record, persist, *args)
- state_index[record.current_state(@name)].call_action(:exit, record)
- if new_state = @events[event].fire(record, *args)
- state_index[new_state].call_action(:enter, record)
-
- if record.respond_to?(event_fired_callback)
- record.send(event_fired_callback, record.current_state, new_state)
- end
-
- record.current_state(@name, new_state, persist)
- record.send(@events[event].success) if @events[event].success
- true
- else
- if record.respond_to?(event_failed_callback)
- record.send(event_failed_callback, event)
- end
-
- false
- end
- end
-
- def states_for_select
- states.map { |st| [st.display_name, st.name.to_s] }
- end
-
- def events_for(state)
- events = @events.values.select { |event| event.transitions_from_state?(state) }
- events.map! { |event| event.name }
- end
-
- def current_state_variable
- "@#{@name}_current_state"
- end
-
- private
- def state(name, options = {})
- @states << (state_index[name] ||= State.new(name, :machine => self)).update(options)
- end
-
- def event(name, options = {}, &block)
- (@events[name] ||= Event.new(self, name)).update(options, &block)
- end
-
- def event_fired_callback
- @event_fired_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_fired'
- end
-
- def event_failed_callback
- @event_failed_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_failed'
- end
- end
- end
-end
diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb
deleted file mode 100644
index 76916b1d86..0000000000
--- a/activemodel/lib/active_model/state_machine/state.rb
+++ /dev/null
@@ -1,47 +0,0 @@
-module ActiveModel
- module StateMachine
- class State
- attr_reader :name, :options
-
- def initialize(name, options = {})
- @name = name
- if machine = options.delete(:machine)
- machine.klass.define_state_query_method(name)
- end
- update(options)
- end
-
- def ==(state)
- if state.is_a? Symbol
- name == state
- else
- name == state.name
- end
- end
-
- def call_action(action, record)
- action = @options[action]
- case action
- when Symbol, String
- record.send(action)
- when Proc
- action.call(record)
- end
- end
-
- def display_name
- @display_name ||= name.to_s.gsub(/_/, ' ').capitalize
- end
-
- def for_select
- [display_name, name.to_s]
- end
-
- def update(options = {})
- if options.key?(:display) then @display_name = options.delete(:display) end
- @options = options
- self
- end
- end
- end
-end
diff --git a/activemodel/lib/active_model/state_machine/state_transition.rb b/activemodel/lib/active_model/state_machine/state_transition.rb
deleted file mode 100644
index b0c5504de7..0000000000
--- a/activemodel/lib/active_model/state_machine/state_transition.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-module ActiveModel
- module StateMachine
- class StateTransition
- attr_reader :from, :to, :options
-
- def initialize(opts)
- @from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition]
- @options = opts
- end
-
- def perform(obj)
- case @guard
- when Symbol, String
- obj.send(@guard)
- when Proc
- @guard.call(obj)
- else
- true
- end
- end
-
- def execute(obj, *args)
- case @on_transition
- when Symbol, String
- obj.send(@on_transition, *args)
- when Proc
- @on_transition.call(obj, *args)
- end
- end
-
- def ==(obj)
- @from == obj.from && @to == obj.to
- end
-
- def from?(value)
- @from == value
- end
- end
- end
-end