Module: Toast::ActiveRecordExtensions

Defined in:
lib/toast/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#is_resourceful_model?Boolean

defaults for non resourceful-models

Returns:

  • (Boolean)


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

def is_resourceful_model?
  false
end

#resource_namesObject



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

def resource_names
  @@all_resourceful_resource_names
end

#resourceful_model(&block) ⇒ Object

Configuration DSL



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/toast/active_record_extensions.rb', line 8

def resourceful_model &block
  @toast_config = Toast::ConfigDSL::Base.new(self)
  Blockenspiel.invoke( block, @toast_config)

  # add class methods
  self.instance_eval do

    cattr_accessor :uri_base

    def is_resourceful_model?
      true
    end

    def toast_config
      @toast_config
    end
  end

  # add instance methods
  self.class_eval do
    # Return toast's standard uri for a record
    def uri
      "#{self.class.uri_base}/#{self.class.to_s.pluralize.underscore}/#{self.id}"
    end

    # Returns a Hash with all exposed attributes
    def exposed_attributes options = {}
      options.reverse_merge! :in_collection => false,
                             :with_uri => true

      # attributes
      exposed_attr =
        options[:in_collection] ? self.class.toast_config.in_collection.exposed_attributes :
                                  self.class.toast_config.exposed_attributes

      out = exposed_attr.inject({}) do |acc, attr|
        acc[attr] = self.send attr
        acc
      end

      # association URIs
      exposed_assoc =
        options[:in_collection] ? self.class.toast_config.in_collection.exposed_associations :
                                  self.class.toast_config.exposed_associations

      exposed_assoc.each do |assoc|
        out[assoc] = self.uri + "/" + assoc
      end

      out["uri"] = self.uri if options[:with_uri]

      out
    end
  end
end

#resourceful_model_optionsObject



68
69
70
# File 'lib/toast/active_record_extensions.rb', line 68

def resourceful_model_options
  nil
end