diff --git a/app/controllers/admin/domains_controller.rb b/app/controllers/admin/domains_controller.rb new file mode 100755 index 0000000..969e962 --- /dev/null +++ b/app/controllers/admin/domains_controller.rb @@ -0,0 +1,65 @@ +class Admin::DomainsController < ApplicationController + layout "admin" + before_action :build_tree, only:[:new, :edit, :index] + + + def index + @domains = Domain.order(:name) + end + + def new + @category = Domain.new() + + end + + def create + @category = Domain.new(domain_params) + + if @category.save + flash[:notice] = "Catégorie créée avec succès." + redirect_to admin_domains_path + else + + render "new" + end + end + + def edit + @category = Domain.find(params[:id]) + + end + + def update + @category = Domain.find(params[:id]) + if @category.update_attributes(domain_params) + flash[:notice] = "Catégorie modifiée avec succès." + redirect_to admin_domains_path + else + + render :action => "edit" + end + end + + def destroy + @category = Domain.find(params[:id]) + if @category.destroy + flash[:notice] = "Catégorie supprimée avec succès." + else + flash[:error] = "Impossible de supprimer cette catégorie." + end + + render "index" + end + + def domain_params + params.require(:domain).permit(:parent_id, :name) + end + + private + + def build_tree + + + end + +end diff --git a/app/controllers/admin/need_categories_controller.rb b/app/controllers/admin/need_categories_controller.rb index 8b0cf81..a2468f6 100755 --- a/app/controllers/admin/need_categories_controller.rb +++ b/app/controllers/admin/need_categories_controller.rb @@ -52,7 +52,7 @@ class Admin::NeedCategoriesController < ApplicationController end def need_category_params - params.require(:need_category).permit(:parent_id, :name) + params.require(:need_category).permit! end private diff --git a/app/controllers/admin/needs_controller.rb b/app/controllers/admin/needs_controller.rb index 6393674..cf31ff5 100755 --- a/app/controllers/admin/needs_controller.rb +++ b/app/controllers/admin/needs_controller.rb @@ -165,7 +165,8 @@ class Admin::NeedsController < ApplicationController private def need_params - params.require(:need).permit(:title, :image_file_id, :description, :category_id, :author_id) + params.require(:need).permit! + #params.require(:need).permit(:domain_ids, :title, :image_file_id, :description, :category_id, :author_id) end def build_category_tree diff --git a/app/controllers/public/needs_controller.rb b/app/controllers/public/needs_controller.rb index 1dd9fe4..58785f2 100755 --- a/app/controllers/public/needs_controller.rb +++ b/app/controllers/public/needs_controller.rb @@ -13,6 +13,7 @@ class Public::NeedsController < ApplicationController # Get only public needs @needs = Need.shared + #@needs = @needs.domain_in(current_customer.domain_ids) # filters default value params[:o] ||= 'created-desc' diff --git a/app/models/customer.rb b/app/models/customer.rb index 40fb2a8..2becb08 100755 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,6 +1,7 @@ class Customer < ActiveRecord::Base - + has_many :domains, :through => :domain_customers + has_many :domain_customers, :dependent => :destroy scope :search, -> (search) { where('name LIKE ? OR firstname LIKE ? OR organisation LIKE ?', "%#{search}%", "%#{search}%", "%#{search}%") diff --git a/app/models/domain.rb b/app/models/domain.rb new file mode 100644 index 0000000..60772ad --- /dev/null +++ b/app/models/domain.rb @@ -0,0 +1,10 @@ +class Domain < ActiveRecord::Base + has_many :needs, :through => :domain_needs + has_many :domain_needs + + has_many :customers, :through => :domain_customers + has_many :domain_customers + + has_many :need_categories, :through => :domain_need_categories + has_many :domain_need_categories +end diff --git a/app/models/domain_customer.rb b/app/models/domain_customer.rb new file mode 100644 index 0000000..a33a426 --- /dev/null +++ b/app/models/domain_customer.rb @@ -0,0 +1,4 @@ +class DomainCustomer < ActiveRecord::Base + belongs_to :domain + belongs_to :customer +end diff --git a/app/models/domain_need.rb b/app/models/domain_need.rb new file mode 100644 index 0000000..d44bf08 --- /dev/null +++ b/app/models/domain_need.rb @@ -0,0 +1,4 @@ +class DomainNeed < ActiveRecord::Base + belongs_to :domain + belongs_to :need +end diff --git a/app/models/domain_need_category.rb b/app/models/domain_need_category.rb new file mode 100644 index 0000000..7989e0d --- /dev/null +++ b/app/models/domain_need_category.rb @@ -0,0 +1,4 @@ +class DomainNeedCategory < ActiveRecord::Base + belongs_to :domain + belongs_to :need_category +end diff --git a/app/models/need.rb b/app/models/need.rb index 0bf5512..e6a2351 100755 --- a/app/models/need.rb +++ b/app/models/need.rb @@ -2,9 +2,15 @@ class Need < ActiveRecord::Base include Workflow + has_many :domains, :through => :domain_needs + has_many :domain_needs, :dependent => :destroy + scope :shared, -> { where(state: ["verified", "negociating", "negociated", "failed"]) } + + + scope :with_wishes_count, -> { joins('left join wishes on wishes.need_id = needs.id') .select('needs.*, count(wishes.id) as wishes_count') @@ -17,8 +23,14 @@ class Need < ActiveRecord::Base } scope :search, -> (search) { - where('title LIKE ? OR description LIKE ?', "%#{search}%", "%#{search}%") + where('needs.title LIKE ? OR needs.description LIKE ?', "%#{search}%", "%#{search}%") } + + scope :domain_in, -> (domain_ids) { + joins(:domains).where('domains.id IN(?)', domain_ids) + } + + after_create :create diff --git a/app/models/need_category.rb b/app/models/need_category.rb index 66e742a..c63825d 100755 --- a/app/models/need_category.rb +++ b/app/models/need_category.rb @@ -1,6 +1,10 @@ class NeedCategory < ActiveRecord::Base attr_accessor :level - + + has_many :domains, :through => :domain_need_categories + has_many :domain_need_categories, :dependent => :destroy + + scope :top, -> { where(parent_id: nil) } diff --git a/app/views/admin/customers/_form.html.haml b/app/views/admin/customers/_form.html.haml index a690a95..230741f 100755 --- a/app/views/admin/customers/_form.html.haml +++ b/app/views/admin/customers/_form.html.haml @@ -6,6 +6,8 @@ =f.inputs do + =f.input :domains, :label => "Domaine : ", :collection => Domain.all, :as => :check_boxes + =f.input :organisation, :label => "Société : " =f.input :name, :label => "Nom : " diff --git a/app/views/admin/domains/_domain.html.haml b/app/views/admin/domains/_domain.html.haml new file mode 100755 index 0000000..0d15fe8 --- /dev/null +++ b/app/views/admin/domains/_domain.html.haml @@ -0,0 +1,7 @@ + +%tr{:id => domain.id} + %td + = domain.name + %td.actions{:style => "width:150px;text-align:right"} + = link_to i(:"trash-o"), [:admin, domain], :data => {:confirm => 'Voulez-vous vraiment supprimer cette catégorie ?'}, :method => :delete + = link_to i(:pencil), edit_admin_domain_path(domain) diff --git a/app/views/admin/domains/_form.html.haml b/app/views/admin/domains/_form.html.haml new file mode 100755 index 0000000..5b898a7 --- /dev/null +++ b/app/views/admin/domains/_form.html.haml @@ -0,0 +1,8 @@ +=semantic_form_for [:admin, @category] do |f| + .content + + + =f.inputs do + =f.input :name, :label => "Nom de la catégorie : " + + .actions= f.submit "Sauvegarder", :class => "btn btn-primary" diff --git a/app/views/admin/domains/edit.html.haml b/app/views/admin/domains/edit.html.haml new file mode 100755 index 0000000..868bf3b --- /dev/null +++ b/app/views/admin/domains/edit.html.haml @@ -0,0 +1,2 @@ +%h1 Modifier une catégorie +=render :partial => "form" diff --git a/app/views/admin/domains/index.html.haml b/app/views/admin/domains/index.html.haml new file mode 100755 index 0000000..1ddd0ab --- /dev/null +++ b/app/views/admin/domains/index.html.haml @@ -0,0 +1,15 @@ + +%table.table.admin_table.table-hover.table-striped + %thead.rows_header + + %tr + %th + Nom + %th{:style => "width:100px"} +   + + %tbody.rows + =render @domains + +%br += link_to "Ajouter un domaine", new_admin_domain_path, class:"btn btn-primary" diff --git a/app/views/admin/domains/new.html.haml b/app/views/admin/domains/new.html.haml new file mode 100755 index 0000000..4e9bd95 --- /dev/null +++ b/app/views/admin/domains/new.html.haml @@ -0,0 +1,2 @@ +%h1 Ajouter un domaine +=render :partial => "admin/domains/form" diff --git a/app/views/admin/need_categories/_form.html.haml b/app/views/admin/need_categories/_form.html.haml index 38e0e70..bd9c28e 100755 --- a/app/views/admin/need_categories/_form.html.haml +++ b/app/views/admin/need_categories/_form.html.haml @@ -3,6 +3,8 @@ =f.inputs do + =f.input :domains, :label => "Domaine : ", :collection => Domain.all, :as => :check_boxes + =f.input :parent_id, :as => :select, :collection => @tree.map{|c| [(c.level > 0 ? ('   ' * (c.level - 1)) + "|- ": "").html_safe + c.name, c.id]}, label: "Catégorie parente" =f.input :name, :label => "Nom de la catégorie : " diff --git a/app/views/admin/needs/_form.html.haml b/app/views/admin/needs/_form.html.haml index b8b4811..9a237ec 100755 --- a/app/views/admin/needs/_form.html.haml +++ b/app/views/admin/needs/_form.html.haml @@ -11,6 +11,8 @@ =f.input :description, :label => "Description : ", :rows => 5, :input_html => {:style => "height:100px;"} =f.input :image_file_id, :label => "Image", :as => :qi_image_select =f.input :author, :label => "Auteur", include_blank: "Administrateur" + + =f.input :domains, :label => "Domaine : ", :collection => Domain.all, :as => :check_boxes -#= f.input :note, :label => "Informations supplémentaires pour Négos (Si vous n'avez pas attaché de proposition, merci de préciser son prix dans ce champ)", :rows => 5, :input_html => {:style => "height:100px;"} .actions diff --git a/app/views/layouts/admin.html.haml b/app/views/layouts/admin.html.haml index 99eb157..0dcfe97 100755 --- a/app/views/layouts/admin.html.haml +++ b/app/views/layouts/admin.html.haml @@ -49,6 +49,7 @@ %li= link_to " Besoins", admin_needs_path %li= link_to " Catégories", admin_need_categories_path + %li= link_to " Domaines", admin_domains_path %li= link_to " Offres", admin_offers_path - unread_messages = ContactMessage.where(read_by_admin: false).count -if unread_messages > 0 diff --git a/config/routes.rb b/config/routes.rb index 382e634..f1133fe 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -224,7 +224,7 @@ Rails.application.routes.draw do end namespace :admin do - + resources :domains resources :annonces do end diff --git a/db/schema.rb b/db/schema.rb index 9dd27d5..c181a0d 100755 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160413101451) do +ActiveRecord::Schema.define(version: 20160530213935) do create_table "accepted_offers", force: :cascade do |t| t.datetime "created_at", null: false @@ -248,6 +248,46 @@ ActiveRecord::Schema.define(version: 20160413101451) do t.datetime "updated_at", null: false end + create_table "domain_customers", force: :cascade do |t| + t.integer "domain_id", limit: 4 + t.integer "customer_id", limit: 4 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "domain_customers", ["customer_id"], name: "index_domain_customers_on_customer_id", using: :btree + add_index "domain_customers", ["domain_id"], name: "index_domain_customers_on_domain_id", using: :btree + + create_table "domain_need_categories", force: :cascade do |t| + t.integer "domain_id", limit: 4 + t.integer "need_category_id", limit: 4 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "domain_need_categories", ["domain_id"], name: "index_domain_need_categories_on_domain_id", using: :btree + add_index "domain_need_categories", ["need_category_id"], name: "index_domain_need_categories_on_need_category_id", using: :btree + + create_table "domain_needs", force: :cascade do |t| + t.integer "domain_id", limit: 4 + t.integer "need_id", limit: 4 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "domain_needs", ["domain_id"], name: "index_domain_needs_on_domain_id", using: :btree + add_index "domain_needs", ["need_id"], name: "index_domain_needs_on_need_id", using: :btree + + create_table "domains", force: :cascade do |t| + t.string "name", limit: 255 + t.string "slug", limit: 255 + t.boolean "superadmin", limit: 1 + t.boolean "enabled", limit: 1 + t.text "description", limit: 65535 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "download_contents", force: :cascade do |t| t.string "title", limit: 255 t.string "style", limit: 255 @@ -576,4 +616,10 @@ ActiveRecord::Schema.define(version: 20160413101451) do add_foreign_key "customer_newsgroups", "customers" add_foreign_key "customer_newsgroups", "newsgroups" + add_foreign_key "domain_customers", "customers" + add_foreign_key "domain_customers", "domains" + add_foreign_key "domain_need_categories", "domains" + add_foreign_key "domain_need_categories", "need_categories" + add_foreign_key "domain_needs", "domains" + add_foreign_key "domain_needs", "needs" end diff --git a/test/fixtures/domain_customers.yml b/test/fixtures/domain_customers.yml new file mode 100644 index 0000000..8bd1313 --- /dev/null +++ b/test/fixtures/domain_customers.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + domain_id: + customer_id: + +two: + domain_id: + customer_id: diff --git a/test/fixtures/domain_need_categories.yml b/test/fixtures/domain_need_categories.yml new file mode 100644 index 0000000..8d58ba8 --- /dev/null +++ b/test/fixtures/domain_need_categories.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + domain_id: + need_category_id: + +two: + domain_id: + need_category_id: diff --git a/test/fixtures/domain_needs.yml b/test/fixtures/domain_needs.yml new file mode 100644 index 0000000..3b29d46 --- /dev/null +++ b/test/fixtures/domain_needs.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + domain_id: + need_id: + +two: + domain_id: + need_id: diff --git a/test/fixtures/domains.yml b/test/fixtures/domains.yml new file mode 100644 index 0000000..a0d897c --- /dev/null +++ b/test/fixtures/domains.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + slug: MyString + superadmin: false + enabled: false + description: MyText + +two: + name: MyString + slug: MyString + superadmin: false + enabled: false + description: MyText diff --git a/test/models/domain_customer_test.rb b/test/models/domain_customer_test.rb new file mode 100644 index 0000000..9a2d137 --- /dev/null +++ b/test/models/domain_customer_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class DomainCustomerTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/domain_need_category_test.rb b/test/models/domain_need_category_test.rb new file mode 100644 index 0000000..39d165c --- /dev/null +++ b/test/models/domain_need_category_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class DomainNeedCategoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/domain_need_test.rb b/test/models/domain_need_test.rb new file mode 100644 index 0000000..85164e1 --- /dev/null +++ b/test/models/domain_need_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class DomainNeedTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/domain_test.rb b/test/models/domain_test.rb new file mode 100644 index 0000000..d1e1c1f --- /dev/null +++ b/test/models/domain_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class DomainTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end