Class: Bike::Workflow

Inherits:
Object
  • Object
show all
Includes:
I18n
Defined in:
lib/_workflow/_workflow.rb

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Direct Known Subclasses

Attachment, Blog, Contact, Forum, Register

Defined Under Namespace

Modules: SD Classes: Attachment, Blog, Contact, Forum, Register

Constant Summary collapse

DEFAULT_META =
{
  :item_label => Bike::I18n.n_('item', 'items', 1),
}
DEFAULT_SUB_ITEMS =
{}
ROLE_ADMIN =
0b10000
ROLE_GROUP =
0b01000
ROLE_OWNER =
0b00100
ROLE_USER =
0b00010
ROLE_NONE =
0b00001
PERM =
{
  :create => 0b11111,
  :read   => 0b11111,
  :update => 0b11111,
  :delete => 0b11111,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18n

_, bindtextdomain, domain, domain=, find_msg, lang, lang=, merge_msg!, msg, n_, parse_msg, po_dir, po_dir=

Constructor Details

#initialize(f) ⇒ Workflow

Returns a new instance of Workflow.



67
68
69
# File 'lib/_workflow/_workflow.rb', line 67

def initialize(f)
  @f = f
end

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



65
66
67
# File 'lib/_workflow/_workflow.rb', line 65

def f
  @f
end

Class Method Details

.instance(f) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/_workflow/_workflow.rb', line 50

def self.instance(f)
  klass = (f[:sd] && f[:sd][:workflow]).to_s.capitalize
  if klass != ''
    self.const_get(klass).new f
  else
    self.new f
  end
end

.roles(roles) ⇒ Object



59
60
61
62
63
# File 'lib/_workflow/_workflow.rb', line 59

def self.roles(roles)
  %w(admin group owner user none).select {|r|
    roles & const_get("ROLE_#{r.upcase}") > 0
  }.collect{|r| Bike::I18n._ r }
end

Instance Method Details

#call(method, params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/_workflow/_workflow.rb', line 83

def call(method, params)
  (method == 'post') ? post(params) : get(params)
rescue Bike::Error::Forbidden
  if params[:action] && Bike.client == 'nobody'
    params[:dest_action] ||= (method == 'post') ? :index : params[:action]
    params[:action] = :login
  end
  Bike::Response.unprocessable_entity(:body => __g_default(params)) rescue Bike::Response.forbidden
# TODO: rescue Error::System etc.
end

#default_metaObject



75
76
77
# File 'lib/_workflow/_workflow.rb', line 75

def default_meta
  self.class.const_get :DEFAULT_META
end

#default_sub_itemsObject



71
72
73
# File 'lib/_workflow/_workflow.rb', line 71

def default_sub_items
  self.class.const_get :DEFAULT_SUB_ITEMS
end

#get(params) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/_workflow/_workflow.rb', line 94

def get(params)
  if @f.is_a? Bike::File
    body = (params[:sub_action] == :small) ? @f.thumbnail : @f.body
    Bike::Response.ok(
      :headers => {
        'Content-Type'   => @f.val['type'],
        'Content-Length' => body.to_s.size.to_s,
      },
      :body    => body
    )
  else
    m = "_g_#{params[:action]}"
    respond_to?(m, true) ? __send__(m, params) : _g_default(params)
  end
end

#permit?(roles, action) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/_workflow/_workflow.rb', line 115

def permit?(roles, action)
  case action
    when :login, :done, :message
      true
    when :preview
      # TODO: permit?(roles, action, sub_action = nil)
      (roles & self.class.const_get(:PERM)[:read].to_i) > 0
    else
      (roles & self.class.const_get(:PERM)[action].to_i) > 0
  end
end

#post(params) ⇒ Object



110
111
112
113
# File 'lib/_workflow/_workflow.rb', line 110

def post(params)
  m = "_p_#{params[:action]}"
  respond_to?(m, true) ? __send__(m, params) : _p_default(params)
end

#sd_moduleObject



79
80
81
# File 'lib/_workflow/_workflow.rb', line 79

def sd_module
  self.class.const_get :SD
end