Skip to content

Commit

Permalink
Merge pull request #19 from frywer/fix_api
Browse files Browse the repository at this point in the history
Fix & extend api, new permission
  • Loading branch information
frywer authored May 16, 2020
2 parents b776881 + 5aed7b2 commit 4923fd1
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 162 deletions.
9 changes: 4 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
group :development do
gem 'webmock'
group :test do
gem 'factory_bot_rails'
gem 'rspec-rails'
gem 'poltergeist'
gem 'database_cleaner', '~> 1.6.1'
gem 'rspec-retry'
gem 'database_cleaner'
gem 'rails-controller-testing'
end

gem 'awesome_nested_set'
gem 'jbuilder'
1 change: 0 additions & 1 deletion app/controllers/custom_tables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def show
@entity_count = scope.count
@entity_pages = Paginator.new @entity_count, per_page_option, params['page']
@custom_entities ||= scope.offset(@entity_pages.offset).limit(@entity_pages.per_page).to_a

respond_to do |format|
format.html {}
format.api {}
Expand Down
15 changes: 9 additions & 6 deletions app/helpers/custom_entities_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ def render_custom_entity_notes(journal, options={})
end

def render_api_custom_entity(custom_entity, api)
return if custom_entity.custom_values.empty? || custom_value.custom_field.external_name.blank?

custom_entity.custom_values.each do |custom_value|
api.__send__(custom_value.custom_field.external_name, custom_value.value)
end

return if custom_entity.custom_values.empty?
api.id custom_entity.id
custom_entity.custom_field_values.each do |custom_field_value|
custom_field = custom_field_value.custom_field
external_name = custom_field.external_name
value = custom_field_value.value
next unless external_name.present?
api.__send__(external_name, value)
end
end
end
20 changes: 20 additions & 0 deletions app/models/custom_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,24 @@ def value_by_external_name(external_name)
custom_field_values.detect {|v| v.custom_field.external_name == external_name}.try(:value)
end

def external_values=(values)
custom_field_values.each do |custom_field_value|
key = custom_field_value.custom_field.external_name
next unless key.present?
if values.has_key?(key)
custom_field_value.value = values[key]
end
end
@custom_field_values_changed = true
end

def to_h
values = {}
custom_field_values.each do |value|
values[value.custom_field.external_name] = value.value if value.custom_field.external_name.present?
end
values["id"] = id
values
end

end
8 changes: 0 additions & 8 deletions app/views/custom_tables/show.api.rsb

This file was deleted.

3 changes: 3 additions & 0 deletions app/views/custom_tables/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.array! @custom_entities do |entity|
render_api_custom_entity entity, json
end
16 changes: 9 additions & 7 deletions app/views/issues/_custom_tables.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<% if User.current.allowed_to?(:manage_custom_tables, nil, global: true) %>
<% if User.current.allowed_to?(:manage_custom_tables, nil, global: true) || User.current.allowed_to?(:view_custom_tables, nil, global: true) %>
<% issue.project.all_issue_custom_tables(issue).each do |custom_table| %>
<% query = custom_table.query(totalable_all: true) %>
<% query.add_short_filter('issue_id', "=#{issue.id}") %>
<% back_url = issue_path(issue) %>
<hr />
<div class="contextual">
<%= link_to l(:button_add), new_custom_entity_path(
custom_table_id: custom_table.id,
back_url: back_url,
issue_id: issue.id), remote: true %>
</div>
<% if User.current.allowed_to?(:manage_custom_tables, nil, global: true) %>
<div class="contextual">
<%= link_to l(:button_add), new_custom_entity_path(
custom_table_id: custom_table.id,
back_url: back_url,
issue_id: issue.id), remote: true %>
</div>
<% end %>
<p><strong><%= custom_table.name %></strong></p>
<%= render partial: 'query_custom_table', locals: {
query: query,
Expand Down
6 changes: 4 additions & 2 deletions app/views/issues/_query_custom_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
<% if User.current.admin? %>
<%= link_to l(:button_show), custom_entity_path(entity), title: l(:button_show), class: 'icon-only icon-magnifier' %>
<% end %>
<%= link_to l(:button_edit), edit_custom_entity_path(entity, edit_query: true, back_url: back_url), remote: true, title: l(:button_edit), class: 'icon-only icon-edit' %>
<%= link_to l(:button_delete), custom_entity_path(entity, custom_table_id: custom_table, back_url: back_url), data: {confirm: l(:text_are_you_sure)}, method: :delete, title: l(:button_delete), class: 'icon-only icon-del' %>
<% if User.current.allowed_to?(:manage_custom_tables, nil, global: true) %>
<%= link_to l(:button_edit), edit_custom_entity_path(entity, edit_query: true, back_url: back_url), remote: true, title: l(:button_edit), class: 'icon-only icon-edit' %>
<%= link_to l(:button_delete), custom_entity_path(entity, custom_table_id: custom_table, back_url: back_url), data: {confirm: l(:text_are_you_sure)}, method: :delete, title: l(:button_delete), class: 'icon-only icon-del' %>
<% end %>
</td>
</tr>
<% end %>
Expand Down
9 changes: 7 additions & 2 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
name 'Custom Tables plugin'
author 'Ivan Marangoz'
description 'This is a plugin for Redmine'
version '1.0.4'
requires_redmine :version_or_higher => '4.0.0'
version '1.0.5'
requires_redmine :version_or_higher => '3.4.0'
url 'https://github.com/frywer/custom_tables'
author_url 'https://github.com/frywer'

permission :manage_custom_tables, {
custom_entities: [:new, :edit, :create, :update, :destroy, :context_menu, :bulk_edit, :bulk_update],
}, global: true

permission :view_custom_tables, {
custom_entities: [:show],
}, global: true

Redmine::FieldFormat::UserFormat.customized_class_names << 'CustomEntity'
end

Expand Down
86 changes: 50 additions & 36 deletions spec/controllers/custom_entities_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "spec_helper"

describe CustomEntitiesController, logged: true do
render_views

let(:custom_table) { FactoryBot.create(:custom_table) }
let(:custom_fields) { FactoryBot.create_list(:custom_field, 3, custom_table_id: custom_table.id) }
Expand All @@ -11,55 +12,68 @@

include_context 'logged as admin'

it '#show' do
custom_value
get :show, params: { id: custom_entity }
expect(response).to have_http_status(:success)
describe '#show' do
it 'return success response' do
custom_value
get :show, params: { id: custom_entity }
expect(response).to have_http_status(:success)
end
end

it '#new' do
custom_value
get :new, params: { custom_table_id: custom_table.id }
expect(response).to have_http_status(:success)
describe '#new' do
it 'return success response' do
custom_value
get :new, params: { custom_table_id: custom_table.id }
expect(response).to have_http_status(:success)
end
end

it '#create' do
custom_fields
cf_values = custom_fields.inject({}) {|k, v| k[v.id.to_s] = "value#{v.name}"; k}
expect {
post :create, params: { custom_entity: { custom_table_id: custom_table.id, custom_field_values: cf_values } }
}.to change { CustomEntity.count }.by(1)
describe '#create' do
it 'create new entity' do
custom_fields
cf_values = custom_fields.inject({}) {|k, v| k[v.id.to_s] = "value#{v.name}"; k}
expect {
post :create, params: { custom_entity: { custom_table_id: custom_table.id, custom_field_values: cf_values } }
}.to change { CustomEntity.count }.by(1)
end
end

it '#edit' do
get :edit, params: { id: custom_entity }
expect(response).to have_http_status(:success)
describe '#edit' do
it 'return success response' do
get :edit, params: { id: custom_entity }
expect(response).to have_http_status(:success)
end
end

it '#update' do
custom_value
cf_values = custom_entity.custom_fields.inject({}) {|k, v| k[v.id.to_s] = "new_value"; k}
describe '#update' do
it 'updates entity' do
custom_value
cf_values = custom_entity.custom_fields.inject({}) {|k, v| k[v.id.to_s] = "new_value"; k}

expect {
put :update, params: { id: custom_entity, custom_entity: { custom_field_values: cf_values } }
}.to change { custom_entity.reload.custom_values.last.value }
end
expect {
put :update, params: { id: custom_entity, custom_entity: { custom_field_values: cf_values } }
}.to change { custom_entity.reload.custom_values.last.value }
end

it 'update notes' do
expect {
put :update, params: { id: custom_entity, custom_entity: { notes: 'notes' } }
}.to change(Journal, :count)
it 'update notes' do
expect {
put :update, params: { id: custom_entity, custom_entity: { notes: 'notes' } }
}.to change(Journal, :count)
end
end

it '#delete' do
custom_value
expect {delete :destroy, params: { id: custom_entity }}.to change(CustomEntity, :count).by(-1)
describe '#delete' do
it 'remove entity' do
custom_value
expect {delete :destroy, params: { id: custom_entity }}.to change(CustomEntity, :count).by(-1)
end
end

it '#bulk_delete' do
custom_entity
custom_entity_2
expect {delete :destroy, params: { ids: [custom_entity.id, custom_entity_2.id] }}.to change(CustomEntity, :count).by(-2)
describe '#bulk_delete' do
it 'removes list of intities' do
custom_entity
custom_entity_2
expect {delete :destroy, params: { ids: [custom_entity.id, custom_entity_2.id] }}.to change(CustomEntity, :count).by(-2)
end
end

end
82 changes: 55 additions & 27 deletions spec/controllers/custom_tables_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,76 @@
require "spec_helper"

describe CustomTablesController, logged: true do
render_views

include_context 'logged as admin'

let(:custom_table) { FactoryBot.create(:custom_table) }
let(:custom_table) { FactoryBot.create(:custom_table, custom_fields: [
{ external_name: 'name', field_format: 'string', is_required: true },
{ external_name: 'age', field_format: 'int' },
{ field_format: 'float' }
] ) }

it '#index' do
get :index
expect(response).to have_http_status(:success)
end
let!(:custom_entity) { FactoryBot.create(:custom_entity, custom_table: custom_table, external_values: {
'name' => 'Bob',
'age' => '24'
} ) }

it '#show' do
get :show, params: { id: custom_table }
expect(response).to have_http_status(:success)
describe '#index' do
it 'return success response' do
get :index
expect(response).to have_http_status(:success)
end
end

it '#new' do
get :new
expect(response).to have_http_status(:success)
describe '#show' do
it 'return success response' do
get :show, params: { id: custom_table }
expect(response).to have_http_status(:success)
end

it 'returns json' do
get :show, params: { id: custom_table, format: 'json' }
expect(response).to have_http_status(:success)
end
end

it '#create' do
expect {
post :create, params: { custom_table: { name: 'Contacts'} }
}.to change { CustomTable.count }.by(1)
describe '#new' do
it 'return success response' do
get :new
expect(response).to have_http_status(:success)
end
end

it '#edit' do
get :edit, params: { id: custom_table }
expect(response).to have_http_status(:success)
describe '#create' do
it 'create new entity' do
expect {
post :create, params: { custom_table: { name: 'Contacts'} }
}.to change { CustomTable.count }.by(1)
end
end

it '#update' do
expect {
put :update, params: { id: custom_table.id, custom_table: { name: 'Contacts updated'} }
}.to change { custom_table.reload.name }
describe '#edit' do
it 'return success response' do
get :edit, params: { id: custom_table }
expect(response).to have_http_status(:success)
end
end

it '#destroy' do
custom_table
expect {
delete :destroy, params: { id: custom_table.id }
}.to change { CustomTable.count }
describe '#update' do
it 'updates entity' do
expect {
put :update, params: { id: custom_table.id, custom_table: { name: 'Contacts updated'} }
}.to change { custom_table.reload.name }
end
end

describe '#destroy' do
it 'destroy entity' do
custom_table
expect {
delete :destroy, params: { id: custom_table.id }
}.to change { CustomTable.count }
end
end
end
Loading

0 comments on commit 4923fd1

Please sign in to comment.