Module: DataMapper::Is::Published::ClassMethods

Defined in:
lib/is/published.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#publish_statesObject (readonly)

Returns the value of attribute publish_states.



184
185
186
# File 'lib/is/published.rb', line 184

def publish_states
  @publish_states
end

#publish_states_for_validationObject (readonly)

Returns the value of attribute publish_states_for_validation.



184
185
186
# File 'lib/is/published.rb', line 184

def publish_states_for_validation
  @publish_states_for_validation
end

Instance Method Details

#all(*args) ⇒ Object

Overriding the normal #all method to add some extra sugar.

Examples

Article.all  => returns all Articles as usual

Article.all( :publish_status => :live )  => returns all Articles with :publish_status == :lve

Article.all(:draft)  => returns all Articles with :publish_status == :draft

Article.all(:draft, :author => @author_joe )  => finds all Articles with :publish_status = :draft and author == Joe


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/is/published.rb', line 216

def all(*args)
  # incoming can either be:  
  # -- nil (nothing passed in, so just use super )
  # -- (Hash)  => all(:key => "value") ( normal operations, so just pass on the Hash ) 
  # -- (Symbol)  => all(:draft) ( just get the symbol, )
  # -- (Symbol, Hash )  => :draft, { extra options }
  # -- (DataMapper somethings) => leave it alone
  if args.empty?
    return super 
  elsif args.first.is_a?(Symbol)
    # set the from args Array, remove first item if Symbol, and then check for 2nd item's presence
    state, options = args.shift.to_s, (args.blank? ? {} : args.first) 
    # puts " and state=[#{state}] and options=[#{options.class}] options.inspect=[#{options.inspect}] [#{__FILE__}:#{__LINE__}]"
    return super({ :publish_status => state }.merge(options) )
  elsif args.first.is_a?(Hash)
    # puts "dm-is-published args.first was a HASH ] [#{__FILE__}:#{__LINE__}]"
    return super(args.first)
  else
    # puts "dm-is-published (ELSE) [#{__FILE__}:#{__LINE__}]"
    return super
  end
end

#publish_states_as_jsonObject

Returns a JSON representation of the publish states, where each state is represented as a lowercase key and an uppercase value.

Examples

Model.publish_states_as_json  

  => { 'live': 'LIVE','draft': 'DRAFT','hidden': 'HIDDEN' }


197
198
199
# File 'lib/is/published.rb', line 197

def publish_states_as_json
  %Q[{ #{self.publish_states_for_validation.collect{ |state| "'#{state.to_s.downcase}': '#{state.to_s.upcase}'" }.join(',')} }]
end