Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions lib/upmin/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,9 @@ def type
end

def editable?
case name.to_sym
when :id
return false
when :created_at
return false
when :created_on
return false
when :updated_at
return false
when :updated_on
if model.uneditable_attributes.include? name
return false
else
# TODO(jon): Add a way to declare which attributes are editable and which are not later.
return model.respond_to?("#{name}=")
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/upmin/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def attributes
return @attributes
end

def uneditable_attributes
return @uneditable_attributes if defined?(@uneditable_attributes)
@uneditable_attributes = self.class.uneditable_attributes
return @uneditable_attributes
end

def associations
return @associations if defined?(@associations)
@associations = []
Expand Down Expand Up @@ -189,6 +195,10 @@ def Model.search_path
return Upmin::Engine.routes.url_helpers.upmin_search_path(klass: model_class_name)
end

def Model.default_uneditable_attributes
return [:id, :created_at, :created_on, :updated_at, :updated_on]
end

def Model.color
return @color if defined?(@color)
@color = Model.next_color
Expand Down Expand Up @@ -258,6 +268,18 @@ def Model.attributes(*attributes)
return (@attributes + @extra_attrs).uniq
end

# Sets the uneditable attributes to the provided attributes merged with the
# default uneditable attributes.
def Model.uneditable_attributes(*uneditable_attributes)
@uneditable_attributes = [] unless defined?(@uneditable_attributes)

if uneditable_attributes.any?
@uneditable_attributes = uneditable_attributes.map{|a| a.to_sym}
end

return (default_uneditable_attributes + @uneditable_attributes).uniq
end

# Add a single action to upmin actions. If this is called
# before upmin_actions the actions will not include any defaults
# actions.
Expand Down