From 69715aa6a451a1a387238066639e4a2d9554bd97 Mon Sep 17 00:00:00 2001 From: Lee Dykes Date: Wed, 26 Aug 2015 13:43:42 -0500 Subject: [PATCH] initial commit --- .gitignore | 9 +++++++++ CODE_OF_CONDUCT.md | 13 +++++++++++++ Gemfile | 4 ++++ README.md | 36 ++++++++++++++++++++++++++++++++++++ Rakefile | 1 + bin/console | 14 ++++++++++++++ bin/setup | 7 +++++++ lib/surveyable.rb | 6 ++++++ lib/surveyable/acts_as_response.rb | 16 ++++++++++++++++ lib/surveyable/acts_as_survey.rb | 17 +++++++++++++++++ lib/surveyable/answer.rb | 10 ++++++++++ lib/surveyable/answer_choice.rb | 6 ++++++ lib/surveyable/question.rb | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/surveyable/version.rb | 3 +++ surveyable.gemspec | 32 ++++++++++++++++++++++++++++++++ test/acts_as_survey_test.rb | 23 +++++++++++++++++++++++ 16 files changed, 296 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 CODE_OF_CONDUCT.md create mode 100644 Gemfile create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/console create mode 100755 bin/setup create mode 100644 lib/surveyable.rb create mode 100644 lib/surveyable/acts_as_response.rb create mode 100644 lib/surveyable/acts_as_survey.rb create mode 100644 lib/surveyable/answer.rb create mode 100644 lib/surveyable/answer_choice.rb create mode 100644 lib/surveyable/question.rb create mode 100644 lib/surveyable/version.rb create mode 100644 surveyable.gemspec create mode 100644 test/acts_as_survey_test.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cb6eeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..ce9bee7 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,13 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. + +Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a8a8408 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in surveyable.gemspec +gemspec diff --git a/README.md b/README.md new file mode 100644 index 0000000..1bf7dcc --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Surveyable + +Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/surveyable`. To experiment with that code, run `bin/console` for an interactive prompt. + +TODO: Delete this and the text above, and describe your gem + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem 'surveyable' +``` + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install surveyable + +## Usage + +TODO: Write usage instructions here + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/surveyable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct. + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..ea987d2 --- /dev/null +++ b/bin/console @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "surveyable" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..b65ed50 --- /dev/null +++ b/bin/setup @@ -0,0 +1,7 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/surveyable.rb b/lib/surveyable.rb new file mode 100644 index 0000000..dc51050 --- /dev/null +++ b/lib/surveyable.rb @@ -0,0 +1,6 @@ +require "surveyable/version" +require "acts_as_survey" + +module Surveyable + # Your code goes here... +end diff --git a/lib/surveyable/acts_as_response.rb b/lib/surveyable/acts_as_response.rb new file mode 100644 index 0000000..58efa6f --- /dev/null +++ b/lib/surveyable/acts_as_response.rb @@ -0,0 +1,16 @@ +module Surveyable + module ActsAsResponse + extend ActiveSupport::Concern + included do + has_many :answers, as: :response + end + module ClassMethods + def acts_as_response(survey, options = {}) + cattr_accessor :survey + self.survey = survey + end + end + end +end + +ActiveRecord::Base.send :include, Surveyable::ActsAsResponse \ No newline at end of file diff --git a/lib/surveyable/acts_as_survey.rb b/lib/surveyable/acts_as_survey.rb new file mode 100644 index 0000000..a153441 --- /dev/null +++ b/lib/surveyable/acts_as_survey.rb @@ -0,0 +1,17 @@ +module Surveyable + module ActsAsSurvey + extend ActiveSupport::Concern + included do + has_many :questions, as: :survey + + end + module ClassMethods + def acts_as_survey(responses, options = {}) + cattr_accessor :responses + self.responses = responses + end + end + end +end + +ActiveRecord::Base.send :include, Surveyable::ActsAsSurvey \ No newline at end of file diff --git a/lib/surveyable/answer.rb b/lib/surveyable/answer.rb new file mode 100644 index 0000000..c65d5a6 --- /dev/null +++ b/lib/surveyable/answer.rb @@ -0,0 +1,10 @@ +module Surveyable + class Answer < ActiveRecord::Base + belongs_to :response, polymorphic: true + belongs_to :question + belongs_to :answer_choice + + scope :required, -> {joins(:question).where('questions.required'=>true)} + scope :answered, -> {where("(answers.text IS NOT NULL AND answers.text <> '') OR answers.answer_choice_id IS NOT NULL")} + end +end diff --git a/lib/surveyable/answer_choice.rb b/lib/surveyable/answer_choice.rb new file mode 100644 index 0000000..23f0bd4 --- /dev/null +++ b/lib/surveyable/answer_choice.rb @@ -0,0 +1,6 @@ +module Surveyable + class AnswerChoice < ActiveRecord::Base + belongs_to :question + has_one :answer + end +end diff --git a/lib/surveyable/question.rb b/lib/surveyable/question.rb new file mode 100644 index 0000000..e7f3f36 --- /dev/null +++ b/lib/surveyable/question.rb @@ -0,0 +1,99 @@ +module Surveyable + class Question < ActiveRecord::Base + belongs_to :survey, polymorphic: true + has_many :answers, :dependent => :restrict_with_error + has_many :answer_choices, dependent: :destroy + STI_TYPES = %w[InfoField HiddenField BooleanField DateField SingleSelectField MultiSelectField TextField StringField IntegerField MoneyAmountField SingleDocumentField MultiDocumentField TelephoneField RelationshipSelectField] + accepts_nested_attributes_for :answer_choices, :allow_destroy => true, + :reject_if => lambda { |a| a[:text].blank? } + validates_presence_of :text + validates_presence_of :type + + scope :required, -> { where(required:true)} + def field_type + 'text' + end + end + class InfoField < Question + def field_type + 'noAnswer' + end + end + + + class HiddenField < Question + def field_type + 'hidden' + end + end + + class BooleanField < Question + def field_type + 'boolean' + end + end + + class DateField < Question + def field_type + 'date' + end + end + + class SingleSelectField < Question + def field_type + 'select' + end + end + + class RelationshipSelectField < SingleSelectField + + end + + class MultiSelectField < Question + def field_type + 'select' + end + end + + class TextField < Question + def field_type + 'textarea' + end + end + + class StringField < Question + def field_type + 'text' + end + end + + class IntegerField < Question + def field_type + 'number' + end + end + + class MoneyAmountField < Question + def field_type + 'text' + end + end + + class SingleDocumentField < Question + def field_type + 'file' + end + end + + class MultiDocumentField < Question + def field_type + 'file' + end + end + + class TelephoneField < Question + def field_type + 'tel' + end + end +end \ No newline at end of file diff --git a/lib/surveyable/version.rb b/lib/surveyable/version.rb new file mode 100644 index 0000000..8d795ee --- /dev/null +++ b/lib/surveyable/version.rb @@ -0,0 +1,3 @@ +module Surveyable + VERSION = "0.1.0" +end diff --git a/surveyable.gemspec b/surveyable.gemspec new file mode 100644 index 0000000..f3a81d0 --- /dev/null +++ b/surveyable.gemspec @@ -0,0 +1,32 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'surveyable/version' + +Gem::Specification.new do |spec| + spec.name = "surveyable" + spec.version = Surveyable::VERSION + spec.authors = ["Lee Dykes"] + spec.email = ["lee.d.dykes@gmail.com"] + + spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.} + spec.description = %q{TODO: Write a longer description or delete this line.} + spec.homepage = "TODO: Put your gem's website or public repo URL here." + + # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or + # delete this section to allow pushing this gem to any host. + if spec.respond_to?(:metadata) + spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" + else + raise "RubyGems 2.0 or newer is required to protect against public gem pushes." + end + + spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.10" + spec.add_development_dependency "rake", "~> 10.0" + spec.add_runtime_dependency "activerecord", "~> 4.0.0" +end diff --git a/test/acts_as_survey_test.rb b/test/acts_as_survey_test.rb new file mode 100644 index 0000000..23f49ed --- /dev/null +++ b/test/acts_as_survey_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' + +class ActsAsSurveyTest < ActiveSupport::TestCase + + # Called before every test method runs. Can be used + # to set up fixture information. + def setup + # Do nothing + end + + # Called after every test method runs. Can be used to tear + # down fixture information. + + def teardown + # Do nothing + end + + # Fake test + def test_fail + + fail('Not implemented') + end +end \ No newline at end of file -- libgit2 0.21.0