Sha256: 46aa2672d1c88773614095fddf4fbb4aeccadcd4a7714c08c1e0bb6442a8ec04

Contents?: true

Size: 1.8 KB

Versions: 29

Compression:

Stored size: 1.8 KB

Contents

module Hobo
  module Model
    module Lifecycles

      class Creator < Struct.new(:lifecycle, :name, :on_create, :options)

        def initialize(*args)
          super
          self.name = name.to_sym
          lifecycle.creators[name] = self
        end

        include Actions

        def allowed?(user)
          record = lifecycle.model.new
          record.with_acting_user(user) { can_run?(record) }
        end


        def candidate(user, attributes=nil)
          record = lifecycle.model.new
          record.with_acting_user(user) { prepare!(record, attributes) }
          record.exempt_from_edit_checks = true
          record
        end


        def extract_attributes(attributes)
          model = lifecycle.model
          params = options.fetch(:params, [])
          allowed = params.dup
          params.each do |p|
            if (refl = model.reflections[p]) && refl.macro == :belongs_to
              allowed << refl.primary_key_name.to_s
              allowed << refl.options[:foreign_type] if refl.options[:polymorphic]
            end
          end
          attributes & allowed
        end


        def change_state(record)
          state = get_state(record, options[:become])
          record.lifecycle.become state if state
        end


        def run!(user, attributes)
          record = lifecycle.model.new
          record.lifecycle.active_step = self
          record.with_acting_user(user) do
            prepare!(record, attributes)
            if can_run?(record)
              if change_state(record)
                fire_event(record, on_create)
              end
              record
            else
              raise Hobo::PermissionDeniedError
            end
          end
        end


        def parameters
          options[:params] || []
        end

      end

    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
hobo-1.3.3 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.2 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.1 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.RC4 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.RC3 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.RC2 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.RC1 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.RC lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre31 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre29 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre28 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre27 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre26 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre25 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre24 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre23 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre22 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre21 lib/hobo/model/lifecycles/creator.rb
hobo-1.3.0.pre20 lib/hobo/model/lifecycles/creator.rb