A simple and general self-consistent theory of coherent spontaneous undulator radiation from electron bunches has been developed. The approach is based on the model of a moving and radiating plane layer filled with oscillating relativistic electrons (gas of radiating and interacting 1-D particles). Using simple expressions for the field of a charged plane makes it easy to consider both Coulomb and radiation interaction of the electrons in bunches with arbitrary particle trajectories, energy chirp, and velocity and density distributions. The application of the developed theory for estimations of a compact radiation source in the terahertz frequency range has demonstrated the possibility of efficiency enhancement and narrowing of the radiation spectrum by means of using bunches with decreasing electron density.