Basic models set up
authorShrey <shrey14099@iiitd.ac.in>
Sat, 16 May 2015 08:38:50 +0000 (14:08 +0530)
committerMatt Amos <zerebubuth@gmail.com>
Mon, 22 Aug 2016 15:12:31 +0000 (16:12 +0100)
Gemfile
Gemfile.lock
app/models/issue.rb [new file with mode: 0644]
app/models/report.rb [new file with mode: 0644]
db/migrate/20150516073616_create_issues.rb [new file with mode: 0644]
db/migrate/20150516075620_create_reports.rb [new file with mode: 0644]
db/structure.sql
test/fixtures/issues.yml [new file with mode: 0644]
test/fixtures/reports.yml [new file with mode: 0644]
test/models/issue_test.rb [new file with mode: 0644]
test/models/report_test.rb [new file with mode: 0644]

diff --git a/Gemfile b/Gemfile
index 07bb60c..25d80f1 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -68,6 +68,9 @@ gem "omniauth-github"
 # Markdown formatting support
 gem "redcarpet"
 
+# For status transitions of Issues
+gem "aasm"
+
 # Load libxml support for XML parsing and generation
 gem "libxml-ruby", ">= 2.0.5", :require => "libxml"
 
index d8c141f..a007125 100644 (file)
@@ -2,6 +2,7 @@ GEM
   remote: https://rubygems.org/
   specs:
     SystemTimer (1.2.3)
+    aasm (4.1.0)
     actionmailer (4.2.7)
       actionpack (= 4.2.7)
       actionview (= 4.2.7)
@@ -307,6 +308,7 @@ PLATFORMS
 
 DEPENDENCIES
   SystemTimer (>= 1.1.3)
+  aasm
   actionpack-page_caching
   autoprefixer-rails
   bigdecimal (~> 1.1.0)
diff --git a/app/models/issue.rb b/app/models/issue.rb
new file mode 100644 (file)
index 0000000..f678e94
--- /dev/null
@@ -0,0 +1,42 @@
+class Issue < ActiveRecord::Base
+       belongs_to :reportable, :polymorphic => true
+       has_many :reports
+       validates :reportable_id, :uniqueness => { :scope => [ :reportable_type ] }
+
+       # Check if more statuses are needed
+       enum status: %w( open ignored resolved )
+
+       scope :with_status, -> (issue_status) { where(:status => statuses[issue_status])}
+
+       def read_reports
+               resolved_at.present? ? reports.where("created_at < ?", resolved_at) : nil
+       end
+
+       def unread_reports
+    resolved_at.present? ? reports.where("created_at >= ?", resolved_at) : reports
+       end
+
+       include AASM
+       aasm :column => :status, :no_direct_assignment => true do
+               state :open, :initial => true
+               state :ignored
+               state :resolved
+
+               event :ignore do
+                       transitions :from => :open, :to => :ignored 
+               end
+
+               event :resolve do
+                       transitions :from => :open, :to => :resolved
+                       after do
+                               self.resolved_at = Time.now.getutc
+                       end
+               end
+
+               event :reopen do
+                       transitions :from => :resolved, :to => :open
+               end
+
+       end
+
+end
diff --git a/app/models/report.rb b/app/models/report.rb
new file mode 100644 (file)
index 0000000..5b238dc
--- /dev/null
@@ -0,0 +1,5 @@
+class Report < ActiveRecord::Base
+       belongs_to :issue
+       belongs_to :user
+       
+end
diff --git a/db/migrate/20150516073616_create_issues.rb b/db/migrate/20150516073616_create_issues.rb
new file mode 100644 (file)
index 0000000..3a59c9e
--- /dev/null
@@ -0,0 +1,16 @@
+class CreateIssues < ActiveRecord::Migration
+  def change
+    create_table :issues do |t|
+      t.string :reportable_type
+      t.integer :reportable_id
+      t.integer :user_id
+      t.integer :status
+      t.datetime :resolved_at
+      t.integer :resolved_by
+      t.datetime :created_at
+      t.datetime :updated_at
+
+      t.timestamps null: false
+    end
+  end
+end
diff --git a/db/migrate/20150516075620_create_reports.rb b/db/migrate/20150516075620_create_reports.rb
new file mode 100644 (file)
index 0000000..bcdc310
--- /dev/null
@@ -0,0 +1,13 @@
+class CreateReports < ActiveRecord::Migration
+  def change
+    create_table :reports do |t|
+      t.integer :issue_id
+      t.integer :user_id
+      t.text :details
+      t.datetime :created_at
+      t.datetime :updated_at
+
+      t.timestamps null: false
+    end
+  end
+end
index f20f1ca..ce03a75 100644 (file)
@@ -663,6 +663,42 @@ CREATE SEQUENCE gpx_files_id_seq
 ALTER SEQUENCE gpx_files_id_seq OWNED BY gpx_files.id;
 
 
+--
+-- Name: issues; Type: TABLE; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE TABLE issues (
+    id integer NOT NULL,
+    reportable_type character varying,
+    reportable_id integer,
+    user_id integer,
+    status integer,
+    resolved_at timestamp without time zone,
+    resolved_by integer,
+    created_at timestamp without time zone NOT NULL,
+    updated_at timestamp without time zone NOT NULL
+);
+
+
+--
+-- Name: issues_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE issues_id_seq
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- Name: issues_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE issues_id_seq OWNED BY issues.id;
+
+
 --
 -- Name: languages; Type: TABLE; Schema: public; Owner: -
 --
@@ -964,6 +1000,39 @@ CREATE TABLE relations (
 );
 
 
+--
+-- Name: reports; Type: TABLE; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE TABLE reports (
+    id integer NOT NULL,
+    issue_id integer,
+    user_id integer,
+    details text,
+    created_at timestamp without time zone NOT NULL,
+    updated_at timestamp without time zone NOT NULL
+);
+
+
+--
+-- Name: reports_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE reports_id_seq
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- Name: reports_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE reports_id_seq OWNED BY reports.id;
+
+
 --
 -- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
 --
@@ -1266,6 +1335,13 @@ ALTER TABLE ONLY gpx_file_tags ALTER COLUMN id SET DEFAULT nextval('gpx_file_tag
 ALTER TABLE ONLY gpx_files ALTER COLUMN id SET DEFAULT nextval('gpx_files_id_seq'::regclass);
 
 
+--
+-- Name: id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY issues ALTER COLUMN id SET DEFAULT nextval('issues_id_seq'::regclass);
+
+
 --
 -- Name: id; Type: DEFAULT; Schema: public; Owner: -
 --
@@ -1308,6 +1384,13 @@ ALTER TABLE ONLY oauth_tokens ALTER COLUMN id SET DEFAULT nextval('oauth_tokens_
 ALTER TABLE ONLY redactions ALTER COLUMN id SET DEFAULT nextval('redactions_id_seq'::regclass);
 
 
+--
+-- Name: id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY reports ALTER COLUMN id SET DEFAULT nextval('reports_id_seq'::regclass);
+
+
 --
 -- Name: id; Type: DEFAULT; Schema: public; Owner: -
 --
@@ -1472,6 +1555,14 @@ ALTER TABLE ONLY gpx_files
     ADD CONSTRAINT gpx_files_pkey PRIMARY KEY (id);
 
 
+--
+-- Name: issues_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
+--
+
+ALTER TABLE ONLY issues
+    ADD CONSTRAINT issues_pkey PRIMARY KEY (id);
+
+
 --
 -- Name: languages_pkey; Type: CONSTRAINT; Schema: public; Owner: -
 --
@@ -1568,6 +1659,14 @@ ALTER TABLE ONLY relations
     ADD CONSTRAINT relations_pkey PRIMARY KEY (relation_id, version);
 
 
+--
+-- Name: reports_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
+--
+
+ALTER TABLE ONLY reports
+    ADD CONSTRAINT reports_pkey PRIMARY KEY (id);
+
+
 --
 -- Name: user_blocks_pkey; Type: CONSTRAINT; Schema: public; Owner: -
 --
@@ -2554,6 +2653,10 @@ INSERT INTO schema_migrations (version) VALUES ('20150111192335');
 
 INSERT INTO schema_migrations (version) VALUES ('20150222101847');
 
+INSERT INTO schema_migrations (version) VALUES ('20150516073616');
+
+INSERT INTO schema_migrations (version) VALUES ('20150516075620');
+
 INSERT INTO schema_migrations (version) VALUES ('21');
 
 INSERT INTO schema_migrations (version) VALUES ('22');
diff --git a/test/fixtures/issues.yml b/test/fixtures/issues.yml
new file mode 100644 (file)
index 0000000..480a656
--- /dev/null
@@ -0,0 +1,21 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  reportable_type: MyString
+  reportable_id: 1
+  user_id: 1
+  status: 1
+  resolved_at: 2015-05-16 13:06:16
+  resolved_by: 1
+  created_at: 2015-05-16 13:06:16
+  updated_at: 2015-05-16 13:06:16
+
+two:
+  reportable_type: MyString
+  reportable_id: 1
+  user_id: 1
+  status: 1
+  resolved_at: 2015-05-16 13:06:16
+  resolved_by: 1
+  created_at: 2015-05-16 13:06:16
+  updated_at: 2015-05-16 13:06:16
diff --git a/test/fixtures/reports.yml b/test/fixtures/reports.yml
new file mode 100644 (file)
index 0000000..12ffbd2
--- /dev/null
@@ -0,0 +1,15 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  issue_id: 1
+  user_id: 1
+  details: MyText
+  created_at: 2015-05-16 13:26:20
+  updated_at: 2015-05-16 13:26:20
+
+two:
+  issue_id: 1
+  user_id: 1
+  details: MyText
+  created_at: 2015-05-16 13:26:20
+  updated_at: 2015-05-16 13:26:20
diff --git a/test/models/issue_test.rb b/test/models/issue_test.rb
new file mode 100644 (file)
index 0000000..c3887b8
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class IssueTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
diff --git a/test/models/report_test.rb b/test/models/report_test.rb
new file mode 100644 (file)
index 0000000..198d9dd
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ReportTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end