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
11 changes: 6 additions & 5 deletions app/views/upmin/models/search.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
.col-md-4
-# TODO(jon): Implement up_search_box
= up_render(@klass)
.new-button-wrapper
%a.btn.btn-block.btn-success{href: upmin_new_model_path(klass: @klass.model_class_name)}
Create a new
= @klass.humanized_name(:singular)
%br
- if @klass.crud_actions.include? :new
.new-button-wrapper
%a.btn.btn-block.btn-success{href: upmin_new_model_path(klass: @klass.model_class_name)}
Create a new
= @klass.humanized_name(:singular)
%br
8 changes: 5 additions & 3 deletions app/views/upmin/partials/models/_model.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
%h3
= model.title

= link_to(model.path, method: :delete, class: "btn btn-sm btn-danger delete pull-right", title: "Delete #{model.title}.", data: {confirm: "Are you sure?"}) do
%span.glyphicon.glyphicon-trash.white
- if model.crud_actions.include? :destroy
= link_to(model.path, method: :delete, class: "btn btn-sm btn-danger delete pull-right", title: "Delete #{model.title}.", data: {confirm: "Are you sure?"}) do
%span.glyphicon.glyphicon-trash.white

%br
%h3{style: "color: #333;"}
Expand All @@ -18,7 +19,8 @@
- model.attributes.each do |attribute|
= up_render(attribute, locals: { form_builder: f })

= f.submit("Save", class: "btn btn-primary")
- if model.crud_actions.include? :create
= f.submit("Save", class: "btn btn-primary")

- if model.associations.any?
%br
Expand Down
4 changes: 4 additions & 0 deletions lib/upmin/active_record/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def default_attributes
return model_class.attribute_names.map(&:to_sym)
end

def default_crud_actions
return [:index, :create, :new, :edit, :show, :update, :destroy]
end

def attribute_type(attribute)
adapter = model_class.columns_hash[attribute.to_s]
if adapter
Expand Down
23 changes: 23 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 crud_actions
return @crud_actions if defined?(@crud_actions)
@crud_actions = self.class.crud_actions
return @crud_actions
end

def associations
return @associations if defined?(@associations)
@associations = []
Expand Down Expand Up @@ -258,6 +264,18 @@ def Model.attributes(*attributes)
return (@attributes + @extra_attrs).uniq
end

# Sets the CRUD actions to the provided attributes if any are provided.
# If no actions are provided then the actions are set to the default
# actions of the model class.
def Model.crud_actions(*crud_actions)
if crud_actions.any?
@crud_actions = crud_actions.map{|a| a.to_sym}
end
@crud_actions ||= default_crud_actions

return @crud_actions
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 Expand Up @@ -304,6 +322,11 @@ def Model.default_attributes
return default_attributes
end

def Model.default_crud_actions
new
return default_crud_actions
end

def Model.attribute_type(attribute)
new
return attribute_type(attribute)
Expand Down