Module: ASModel::ModelScoping::ClassMethods

Defined in:
backend/app/model/ASModel_scoping.rb

Instance Method Summary collapse

Instance Method Details

#active_repositoryObject



194
195
196
197
198
199
200
201
202
# File 'backend/app/model/ASModel_scoping.rb', line 194

def active_repository
  repo = RequestContext.get(:repo_id)

  if model_scope == :repository and repo.nil?
    raise "Missing repo_id for request!"
  end

  repo
end

#enable_suppressionObject



75
76
77
# File 'backend/app/model/ASModel_scoping.rb', line 75

def enable_suppression
  @suppressible = true
end

#enforce_suppression?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'backend/app/model/ASModel_scoping.rb', line 80

def enforce_suppression?
  RequestContext.get(:enforce_suppression)
end

#handle_publish_flag(ids, val) ⇒ Object



101
102
103
# File 'backend/app/model/ASModel_scoping.rb', line 101

def handle_publish_flag(ids, val)
  ASModel.update_publish_flag(self.filter(:id => ids), val)
end

#handle_suppressed(ids, val) ⇒ Object



89
90
91
92
93
# File 'backend/app/model/ASModel_scoping.rb', line 89

def handle_suppressed(ids, val)
  if suppressible?
    ASModel.update_suppressed_flag(self.filter(:id => ids), val)
  end
end

#model_scope(noerror = false) ⇒ Object



167
168
169
170
171
172
173
174
# File 'backend/app/model/ASModel_scoping.rb', line 167

def model_scope(noerror = false)
  @model_scope or
    if noerror
      nil
    else
      raise "set_model_scope definition missing for model #{self}"
    end
end

#parse_reference(uri, opts) ⇒ Object

Like JSONModel.parse_reference, but enforce repository restrictions



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'backend/app/model/ASModel_scoping.rb', line 178

def parse_reference(uri, opts)
  ref = JSONModel.parse_reference(uri, opts)

  return nil if !ref

  # If the current model is repository scoped, and the reference is a
  # repository-scoped URI, make sure they're talking about the same
  # repository.
  if self.model_scope == :repository && ref[:repository] && ref[:repository] != JSONModel(:repository).uri_for(active_repository)
    raise ReferenceError.new("Invalid URI reference for this (#{active_repository}) repo: '#{uri}'")
  end

  ref
end

#publishable?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'backend/app/model/ASModel_scoping.rb', line 96

def publishable?
  self.columns.include?(:publish)
end

#set_model_scope(value) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'backend/app/model/ASModel_scoping.rb', line 106

def set_model_scope(value)
  if ![:repository, :global].include?(value)
    raise "Failure for #{self}: Model scope must be set as :repository or :global"
  end

  if value == :repository
    model = self
    orig_ds = self.dataset.clone

    # Fetch the request row but blow up if the row isn't from the currently active repository
    orig_ds.with_row_proc(
      proc do |row|
        if row.has_key?(:repo_id) && row[:repo_id] != model.active_repository
          raise ("ASSERTION FAILED: #{row.inspect} has a repo_id of " +
                "#{row[:repo_id]} but the active repository is #{model.active_repository}")
        end
      end
    )

    # Provide a new '.this_repo' method on this model class that only
    # returns records that belong to the current repository.
    def_dataset_method(:this_repo) do
      filter = model.columns.include?(:repo_id) ? {:repo_id => model.active_repository} : {}

      if model.suppressible? && model.enforce_suppression?
        filter[Sequel.qualify(model.table_name, :suppressed)] = 0
      end

      orig_ds.filter(filter)
    end


    # And another that will return records from any repository
    def_dataset_method(:any_repo) do
      if model.suppressible? && model.enforce_suppression?
        orig_ds.filter(Sequel.qualify(model.table_name, :suppressed) => 0)
      else
        orig_ds
      end
    end

  else
    # Globally scoped models

    # These accessors are redundant, but useful in cases where we're
    # working with model classes and don't know/care whether they're
    # repository-scoped or not.  For example, when we're resolving URIs...
    def_dataset_method(:any_repo) do
      self
    end

    def_dataset_method(:this_repo) do
      self
    end

  end

  @model_scope = value
end

#suppressible?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'backend/app/model/ASModel_scoping.rb', line 85

def suppressible?
  @suppressible
end

#uri_for(jsonmodel, id, opts = {}) ⇒ Object



205
206
207
# File 'backend/app/model/ASModel_scoping.rb', line 205

def uri_for(jsonmodel, id, opts = {})
  JSONModel(jsonmodel).uri_for(id, opts.merge(:repo_id => self.active_repository))
end